forked from divyanshu-rawat/haversine-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (38 loc) · 1.25 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
46
47
48
49
50
51
52
53
54
55
56
57
58
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var morgan = require('morgan')
var jwt = require('jsonwebtoken');
var router = express.Router();
var port = process.env.PORT || 4000;
var User = require('./api/models/User');
var Geojson = require('./api/models/geojson');
var config = require('./config');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("Connected correctly to server");
});
// secret variable
// app.set('secretKey', config.secret);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// using morgan to log requests to the console
app.use(morgan('dev'));
app.use('/api', router);
var routes = require('./api/routes/routes');
routes(router);
app.use(function(req, res) {
res.status(404).send({url: req.originalUrl + ' not found'})
});
app.set('port', (process.env.PORT || 5000));
app.listen(port);
console.log('Server Started on Port: ' + port);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});