-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (39 loc) · 1.29 KB
/
Copy pathserver.js
File metadata and controls
48 lines (39 loc) · 1.29 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
require('dotenv/config')
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const bodyParser = require('body-parser');
const routes = require('./routes');
const cors = require('cors');
//cors
//chrome extension for postman
const whitelist = ['chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'http://192.168.100.49',
'http://192.168.100.24',
'http://192.168.100.82']
let corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions));
//middleware for show information about user who request to this server
const logger = function(req, res, next){
console.log('==============================')
console.log('Request URL:', req.originalUrl)
console.log("Request type: " + req.method)
console.log("Request device: " + req.get('User-Agent'))
console.log("Request time: " + new Date())
console.log('==============================')
next()
}
app.use(logger)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
routes(app);
app.listen(port);
console.log('Simple Note APP, RESTful API server started on: ' + port);