-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.mjs
64 lines (53 loc) · 1.6 KB
/
app.mjs
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 path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import helmet from 'helmet';
import kue from 'kue';
import restAPI from './routes/api';
import webAPI from './routes/web';
import healthcheckAPI from './routes/healthcheck';
const app = express();
// Register middlewares:
app.use(helmet());
app.use(express.static(path.join(path.resolve('.'), 'views')));
app.use(express.static(path.join(path.resolve('.'), 'projects'))); /** @todo: Expose data somewhere else */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Configure application:
kue.app.set('title', 'Job queue');
kue.app.disable('x-powered-by');
app.set('view engine', 'ejs');
// Register routes:
app.use('/', webAPI);
app.use('/api', restAPI);
app.use('/queue', kue.app);
app.use('/healthcheck', healthcheckAPI);
// Catch 404 Errors and forward them to the error handler:
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error Handlers:
// Development Error Handler. Will print Stack Trace:
if (app.get('env') === 'development') {
app.use((err, req, res, next) => {
res
.status(err.status || 500)
.json({
message: err.message,
error: err
});
});
} else {
// Production Error Handler. No Stack Trace leaked to User:
app.use((err, req, res, next) => {
res
.status(err.status || 500)
.json({
message: err.message,
error: {}
});
});
}
export default app;