-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (47 loc) · 1.57 KB
/
server.js
File metadata and controls
60 lines (47 loc) · 1.57 KB
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');
const bodyParser = require('body-parser');
const config = require('app-config');
const cors = require("cors");
const helmet = require("helmet");
const mongoose = require('mongoose');
const app = express();
const port = 5000;
// DB connection
mongoose.connect(config.db.url,
{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(()=> console.log('DB connected successfully.')
).catch(err => console.log('DB connection failed ' + err)
);
// This will allow all the routes to be accessed anywhere on the web
//app.use(cors())
const allowlist = [''];
const corsOptionsDelegate = (req, callback) => {
let corsOptions;
let isDomainAllowed = allowlist.indexOf(req.header('Origin')) !== -1;
if (isDomainAllowed) {
// Enable CORS for this request
corsOptions = { origin: true }
} else {
// Disable CORS for this request
corsOptions = { origin: false }
}
callback(null, corsOptions)
}
app.use(cors(corsOptionsDelegate));
app.use(helmet());
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/', require('./routes'));
// If no route is matched by now, it must be a 404
app.use(function(req, res, next) {
const err = new Error('Not Found');
res.status(404).send('Service Not Found 404');
err.status = 404;
next(err);
});
const server = app.listen(port, () => console.log('Server is up and running'));