-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
43 lines (34 loc) · 1.05 KB
/
app.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
var express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const responseTime = require('response-time')
const routes = require('./routes')
const app = express()
var corsOptions = {
origin: '*',
optionsSuccessStatus: 200
};
//create a cors middleware
app.use(function(req, res, next) {
//set headers to allow cross origin request.
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(cors(corsOptions))
app.options('*', cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(responseTime())
app.use('/', routes)
app.use((req, res, next) => {
const err = new Error('Not Found')
err.status = 404
next(err)
})
// Start the server...
const server = app.listen(process.env.PORT || 5000, () => {
console.log(`Listening on port ${server.address().port}`)
})
module.exports = server