forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (38 loc) · 1.16 KB
/
index.js
File metadata and controls
41 lines (38 loc) · 1.16 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
import express from 'express';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import * as controllers from '/controllers';
import { ClientError, ForbiddenError, NotFoundError, UnauthorizedError } from '/common/error';
import webhook from '/common/webhook';
import hierarchy from '/common/hierarchy';
const app = express();
app.use(morgan('tiny'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Object.keys(controllers).forEach(key => app.use(`/${key}`, controllers[key]));
app.use('/webhook', webhook);
app.use((req, res, next) => next(new NotFoundError()));
app.use((err, req, res, next) => {
const statusMap = [
[UnauthorizedError, 401],
[ForbiddenError, 403],
[NotFoundError, 404],
[ClientError, 400],
[Error, 500],
];
const [, status] = statusMap.find(([Error]) => err instanceof Error);
res.status(status);
res.send(err.message);
console.error(err);
});
app.hierarchy = hierarchy;
webhook.on('algorithm-visualizer', event => {
switch (event) {
case 'push':
process.exit(0);
break;
}
});
export default app;