-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode12_1.js
More file actions
41 lines (36 loc) · 1.04 KB
/
node12_1.js
File metadata and controls
41 lines (36 loc) · 1.04 KB
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
var http = require("http");
var url = require("url");
var portNum = process.argv[2];
//parseしたクエリパラメータをjsonで返却する
function parsetime(time) {
return {
"hour": time.getHours(),
"minute": time.getMinutes(),
"second": time.getSeconds()
};
}
function unixtime(time) {
return { "unixtime": time.getTime() };
}
//サーバー起動
var server = http.createServer(function(req, res) {
if(req.method == "GET") {
res.writeHead(200, { 'Content-type': 'application/json' });
var parseUrl = url.parse(req.url, true);
var time = new Date(parseUrl.query['iso']);
var result;
if(parseUrl.pathname == '/api/parsetime') {
result = parsetime(time);
console.log(result);
} else if(parseUrl.pathname == '/api/unixtime') {
result = unixtime(time);
console.log(result);
}
//結果をJSON文字列の形式に変更
res.write(JSON.stringify(result)+ '');
res.end();
}
});
//サーバ待ち受け
server.listen(portNum);
console.log('app listening on port: ', portNum);