forked from kuryaki/bogotajs-learnyounode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.js
37 lines (28 loc) · 900 Bytes
/
13.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
const { createServer } = require('http');
const { parse: parseUrl } = require('url');
const { parse: parseQuery } = require('querystring');
const port = process.argv[2];
createServer((request, response) => {
const parsedUrl = parseUrl(request.url);
const parsedQuery = parseQuery(parsedUrl.query);
const date = new Date(parsedQuery.iso);
let body;
if (parsedUrl.pathname === '/api/parsetime') {
body = {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
}
}
if (parsedUrl.pathname === '/api/unixtime') {
body = {
unixtime: date.getTime()
}
}
if (body) {
response.writeHead(200, { 'Content-Type': 'application/json' });
return response.end(JSON.stringify(body));
}
response.writeHead(404);
response.end();
}).listen(port);