-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (50 loc) · 2.24 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// load config file before loading other modules
const config = require(`./lib/config`);
// load modules
const authenticate = require('./lib/rest/middleware/authenticate');
const bodyParser = require('body-parser');
const createServer = require('./lib/server');
const createSocket = require('./lib/socket');
const error = require('./lib/rest/middleware/error');
const errors = require('./lib/rest/errors');
const express = require('express');
const helmet = require('helmet');
const limiter = require('./lib/rest/middleware/limit');
const route = require('./lib/rest/router');
const routeSocket = require('./lib/socket/router');
const type = require('./lib/rest/middleware/type');
// initialize Express and routers
const app = express(); // create the Express app
const v0 = express.Router(); // create the Version 0.x router
// app settings
app.enable(`trust proxy`); // trust the Azure proxy server
app.set(`port`, config.port); // set port for the app
// middleware
app.use(helmet()); // basic security features
app.use(limiter); // rate limiting
app.use(bodyParser.json()); // parse JSON data in the request body
app.use(express.static(`public`)); // routing for static files
app.use(error); // adds res.error method to response
if (config.logInfo) { // log requested URLs
const logger = require('./lib/rest/middleware/logger');
app.use(logger);
}
app.use(type); // set req.type
app.use(authenticate.unless({ // authenticate requests to the API
path: [/\/test\//], // don't authenticate test routes
}));
// add routes to routers
route(v0);
// mount routers to their respective version paths
app.use(v0);
app.use(`/v0`, v0);
// add generic error handlers
app.use(errors.notFound);
app.use(errors.serverError);
const server = createServer(app); // create the server
const io = createSocket(server); // create the socket
// add routes to sockets
routeSocket(io.of(`/`));
routeSocket(io.of(`/v0`));
app.io = io; // add IO to app (makes it available to request handlers)
module.exports = app; // export app for testing