-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
41 lines (37 loc) · 1.23 KB
/
app.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
41
const http = require('http');
/** handle GET request */
function getHandler(req, res, reqUrl) {
res.writeHead(200);
res.write('GET parameters: ' + reqUrl.searchParams);
res.end();
}
/** handle POST request */
function postHandler(req, res, reqUrl) {
req.setEncoding('utf8');
req.on('data', (chunk) => {
res.writeHead(200);
res.write('POST parameters: ' + chunk);
res.end();
});
}
/** if there is no related function which handles the request, then show error message */
function noResponse(req, res) {
res.writeHead(404);
res.write('Sorry, but we have no response..\n');
res.end();
}
http.createServer((req, res) => {
// create an object for all redirection options
const router = {
'GET/retrieve-data': getHandler,
'POST/send-data': postHandler,
'default': noResponse
};
// parse the url by using WHATWG URL API
let reqUrl = new URL(req.url, 'http://127.0.0.1/');
// find the related function by searching "method + pathname" and run it
let redirectedFunc = router[req.method + reqUrl.pathname] || router['default'];
redirectedFunc(req, res, reqUrl);
}).listen(8080, () => {
console.log('Server is running at http://127.0.0.1:8080/');
});