-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (31 loc) · 948 Bytes
/
index.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 url = require('url');
function handler(req, res) {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*'
});
const {method} = req;
if(method === 'POST') {
console.log('Handling POST Request!');
const {pathname} = url.parse(req.url);
if(pathname === '/add') {
var rawBody = "";
req.on('data', chunk => {
rawBody += chunk.toString();
});
req.on('end', () => {
const {operandA,operandB} = JSON.parse(rawBody);
const result = operandA + operandB;
res.write(JSON.stringify({
result
}));
res.end();
});
}
} else {
res.end();
}
}
http.createServer(handler).listen(8080);