-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
37 lines (32 loc) · 956 Bytes
/
server.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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const app = express();
const maxRequestBodySize = process.env.MAX_REQUEST_BODY_SIZE || '16mb';
app.use(bodyParser.json({
limit: maxRequestBodySize
}));
app.use(methodOverride('X-HTTP-Method-Override'));
const WorkerThread = require('./Thread');
app.post('/worker/:worker', (req, res, next) => {
if (!req.query.key || req.query.key !== process.env.KEY) {
return res.status(401).send('Unauthorized');
}
return next();
}, (req, res) => {
if (!req.params.worker) {
return res.status(400).send('Unknown worker');
}
try {
new WorkerThread(req.params.worker).start(req.body).then((response) => {
res.json(response);
}).catch((error) => {
res.status(400).send(error.message);
});
}
catch (err) {
res.status(500).send(err.message);
}
});
module.exports = app;