-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
21 lines (21 loc) · 760 Bytes
/
index.js
File metadata and controls
21 lines (21 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module.exports = (serviceRegistry) => {
const server = require('http').createServer((req, res) => {
const [, serviceName, methodName] = req.url.split('/');
const payload = [];
req.on('data', chunk => payload.push(chunk));
req.on('end', () => {
try {
const args = JSON.parse(payload.length === 1 ? payload[0] : Buffer.concat(payload));
const service = serviceRegistry[serviceName];
service[methodName].call(service, args)
.then(result => ({result}), error => ({error}))
.then(response => res.end(JSON.stringify(response)));
} catch (e) {
res.end(JSON.stringify({error: e.message}));
} finally {
res.statusCode = 200;
}
});
});
server.listen(3000);
};