-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.js
More file actions
67 lines (53 loc) · 1.71 KB
/
api-server.js
File metadata and controls
67 lines (53 loc) · 1.71 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
For example:
/api/parsetime?iso=2013-08-10T12:10:15.474Z
The JSON response should contain only 'hour', 'minute' and 'second' properties. For example:
{
"hour": 14,
"minute": 23,
"second": 15
}
Add second endpoint for the path '/api/unixtime' which accepts the same query string but returns UNIX epoch time under the property 'unixtime'. For example:
{ "unixtime": 1376136615474 }
Your server should listen on the port provided by the first argument to your program.
*/
var httpServer = require("http");
var portNumber = Number ( process.argv[2] );
var map = require('through2-map');
var url = require('url');
var parseUrlPoint = "/api/parsetime";
var unixtimePoint = "/api/unixtime";
var server = httpServer.createServer( function ( req,resp){
console.log("Server Created!!");
req.setEncoding('utf8');
resp.writeHead(200, { 'Content-Type': 'application/json' })
var urlParsed = url.parse(req.url,true);
var query = urlParsed.query;
var time = new Date(query.iso);
if ( urlParsed.pathname === parseUrlPoint ){
/*
{
"hour": 14,
"minute": 23,
"second": 15
}
*/
var longFormat = new Object();
longFormat.hour = time.getHours();
longFormat.minute = time.getMinutes();
longFormat.second = time.getSeconds();
resp.write(JSON.stringify(longFormat));
resp.end();
}else if ( urlParsed.pathname === unixtimePoint){
var unTime = new Object();
unTime.unixtime = time.getTime();
/*
{ "unixtime": 1376136615474 }
*/
resp.write(JSON.stringify(unTime));
resp.end();
}else{
console.log(" The end point is not the required!!!");
}
});
server.listen(portNumber);