-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpjason.js
More file actions
40 lines (35 loc) · 1002 Bytes
/
httpjason.js
File metadata and controls
40 lines (35 loc) · 1002 Bytes
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')
const portNumber = process.argv[2]
function isoFormat (date) {
return {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
}
}
function unixFormat (date) {
return { unixtime: date.getTime() }
}
var server = http.createServer(function (req, res) {
let parsed = url.parse(req.url, true)
let time = new Date(parsed.query.iso)
let formatedTime
// console.log(time)
// console.log(parsed.pathname)
if (parsed.pathname === '/api/parsetime') {
formatedTime = isoFormat(time)
// console.log(formatedTime)
} else if (parsed.pathname === '/api/unixtime') {
formatedTime = unixFormat(time)
// console.log(formatedTime)
}
if (formatedTime) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(formatedTime))
} else {
res.writeHead(404)
res.end()
}
})
server.listen(Number(portNumber))