-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.78 KB
/
server.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
42
43
44
45
46
47
48
49
50
51
52
const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url, true);
// GET Endpoint
if (reqUrl.pathname == '/getTimeStories' && req.method === 'GET') {
console.log('Request Type:' +
req.method + ' Endpoint: ' +
reqUrl.pathname);
sampleRequest(req, res);
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/getTimeStories`);
});
sampleRequest = function (req, res) {
const reqUrl = url.parse(req.url, true);
console.log('requrl ', reqUrl);
var response =
[
{
"title": "Saudi King Salman Hospitalized for Medical Tests",
"link": "https://time.com/5868873/saudi-kind-salman-hospitalized/"
},
{
"title": "Iran Executes Man Convicted of Spying on Soleimani",
"link": "https://time.com/5868856/iran-executed-spy-soleimani/"
},
{
"title": "Kim Jong Un Berates Officials Over Hospital Project",
"link": "https://time.com/5868844/kim-jong-un-berate-hospital-officials/"
},
{
"title": "Boy, 12, Driving Stolen Truck Leads Police on Chase",
"link": "https://time.com/5868839/delaware-boy-stolen-truck-police/"
},
{
"title": "N.J. Federal Judge’s Son Killed, Husband Wounded",
"link": "https://time.com/5868834/new-jersey-federal-judge-shooting/"
}];
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
};