-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathindex.js
64 lines (55 loc) · 1.96 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import express from 'express';
import helmet from 'helmet';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import responseTime from 'response-time';
import winston from 'winston';
import logger from './lib/logger';
import settings from './lib/settings';
import security from './lib/security';
import dashboardWebSocket from './lib/dashboardWebSocket';
import ajaxRouter from './ajaxRouter';
import apiRouter from './apiRouter';
const app = express();
const STATIC_OPTIONS = {
maxAge: 31536000000 // One year
};
app.set('trust proxy', 1);
app.use(helmet());
app.get('/images/:entity/:id/:size/:filename', (req, res, next) => {
// A stub of image resizing (can be done with Nginx)
const newUrl = `/images/${req.params.entity}/${req.params.id}/${req.params.filename}`;
req.url = newUrl;
next();
});
app.use(express.static('public/content', STATIC_OPTIONS));
security.applyMiddleware(app);
app.all('*', (req, res, next) => {
// CORS headers
const allowedOrigins = security.getAccessControlAllowOrigin();
const { origin } = req.headers;
if (allowedOrigins === '*') {
res.setHeader('Access-Control-Allow-Origin', allowedOrigins);
} else if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Key, Authorization'
);
next();
});
app.use(responseTime());
app.use(cookieParser(settings.cookieSecretKey));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/ajax', ajaxRouter);
app.use('/api', apiRouter);
app.use(logger.sendResponse);
const server = app.listen(settings.apiListenPort, () => {
const serverAddress = server.address();
winston.info(`API running at http://localhost:${serverAddress.port}`);
});
dashboardWebSocket.listen(server);