-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (45 loc) · 1.58 KB
/
app.js
File metadata and controls
53 lines (45 loc) · 1.58 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
'use strict';
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const routes = require('./routes');
const global = require('./globals');
app.use((req, res, next) => {
if (global.allowConnectionFromAllOrigins) {
res.header('Access-Control-Allow-Origin', '*');
} else {
const origin = req.headers.origin;
if (global.allowedOrigins.indexOf(origin) > -1) {
res.header('Access-Control-Allow-Origin', origin);
}
}
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
// Allow all HTTP methods
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST, PUT, DELETE');
return next();
});
/**
* bodyParser.urlencoded(options)
* Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body
*/
app.use(bodyParser.urlencoded({
extended: true
}));
/**
* bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
// routes are defined here
app.use('/', routes);
// for sending server errors back to the client as json
app.use((err, req, res, next) => {
console.log(err);
res.status(500).send( { error: err.toString() } );
});
// listen for requests
app.listen(global.port, (err) => {
if (err) return console.log(`server could not be started on port ${global.port}`, err);
console.log(`server is listening on ${global.port}`)
});