-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (33 loc) · 1.22 KB
/
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
38
39
40
const http = require('http');
const Router = require('router');
const moment = require('moment');
const { handlePublish, handleConsume } = require('./request/handlers');
const config = require('./config.js');
const router = Router();
router.post('/publish/:queue', handlePublish);
router.get('/consume/:queue', handleConsume);
function fallback(req, res) {
return err => {
res.setHeader('Content-Type', 'application/json');
if (!err) {
res.statusCode = 404;
res.end(JSON.stringify({'status': 'invalid URL'}));
} else {
res.statusCode = 500;
res.end(JSON.stringify({'status': 'error', 'message': err.message}));
}
}
}
function logRequest(req, res) {
res.on('finish', () => {
let timestamp = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZZ');
console.log(`[${timestamp}] ${req.socket.remoteAddress} ${req.method} ${req.url} ${res.statusCode}`);
});
}
const server = http.createServer((req, res) => {
router(req, res, fallback(req, res));
logRequest(req, res);
});
server.listen(config.server_port, config.server_hostname, () => {
console.log(`Server running at http://${config.server_hostname}:${config.server_port}/`);
});