-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
60 lines (47 loc) · 1.47 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
59
60
const express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
logger = require('morgan'),
routes = require('./routes/routes');
const app = express();
//=======================
// MIDDLEWARE
//=======================
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(logger('dev'));
//=======================
// DATABASE CONFIG
//=======================
let mongo_uri;
// for local development
mongo_uri = 'mongodb://localhost/localdb';
// for production
// mongo_uri = process.env.DATABASE_URL;
mongoose.connect(mongo_uri, { useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false, useUnifiedTopology: true })
.then(() => console.log("Database connected"))
.catch(console.log);
//=======================
// ALLOW-CORS
//=======================
// For development
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
next();
});
//=======================
// ROUTES
//=======================
app.use("/routes/", routes);
//=======================
// STARTING THE SERVER
//=======================
app.get('/', (req, res) => {
res.send('Works')
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log('App listening on port ' + port);
});