-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
45 lines (36 loc) · 1.71 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var express = require('express'), // Call express
app = express(), // Define our app using express
port = process.env.PORT || 3000, // Set the port
dbUrl = process.env.MONGODB_URI || 'mongodb://localhost/tasksdb',
mongoose = require('mongoose'), // Call mongoose to interact with a MongoDB(Database) instance
Task = require('./api/models/tasksModel'), // Created model loading here
bodyParser = require('body-parser'); //Middleware to process incoming request body objects
// Mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect(dbUrl);
/* Configure app to use bodyParser()
this will let us get the data from a POST */
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Importing route
var routes = require('./api/routes/tasksRoutes');
//Register the route
routes(app);
// Start the server
app.listen(port);
console.log('RESTful API demo server started on: ' + `http://localhost:${port}/`);
// Get an instance of the express Router
var router = express.Router();
// Health route to make sure everything is working (accessed at GET http://localhost:3000/health)
app.use('/health', require('express-healthcheck')({
healthy: function () {
return { message: 'ExpressJS web service is up and running' };
}
}));
// All of our routes will be prefixed with /api
app.use('/api', router);
// Returning response with 404 when incorrect url is requested
app.use(function(req, res) {
res.status(404).send({ error: { errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found',
description: 'Couldn\'t find the requested resource \'' + req.originalUrl + '\'' } ], code: 404, message: 'Not Found' } })
});