-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.js
47 lines (35 loc) · 1.24 KB
/
Index.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
var express = require('express')
var bodyParser = require("body-parser");
var sample = require('./sample/router.js');
var basic = require('./basic/router.js');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.all('/*', function (req, res, next) {
// CORS headers
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
//list of api modules with routes linking
app.use('/sample', sample);
app.use('/basic',basic);
// If no route is matched by now, it must be a 404
app.use(function (req, res, next) {
//var err = new Error('Not Found');
res.status = 404;
res.json({"Message": "Route not found"});
});
//Error handling
app.use(function (err, req, res, next) {
// logic
res.status(500).json({"Message": 'Something broke!'});
console.time().error(err.stack);
});
module.exports = app