-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·42 lines (35 loc) · 1.34 KB
/
server.js
File metadata and controls
executable file
·42 lines (35 loc) · 1.34 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
var http = require('http'),
fs = require('fs'),
url = require('url'),
port = 8080;
/* Global variables */
var listingData, server;
var requestHandler = function(request, response) {
var parsedUrl = url.parse(request.url);
if(url.parse(request.url).pathname == '/listings')
{
response.write(listingData);
response.end();
}
else{
response.writeHead(404, {"Content-Type": "text/plain"}); // sends response header to request
response.write("Bad gateway error");
response.end(); // signals that all has been sent . Must be called on each response
}
/*
Your request handler should send listingData in the JSON format if a GET request
is sent to the '/listings' path. Otherwise, it should send a 404 error.
HINT: explore the request object and its properties
http://stackoverflow.com/questions/17251553/nodejs-request-object-documentation
*/
};
fs.readFile('listings.json', 'utf8', function(err, data) {
/*
This callback function should save the data in the listingData variable,
then start the server.
*/
server = http.createServer(requestHandler); // creater server and listen this takes a callback
//function which in this case is a var that holds the requestHandler on top
listingData = data; // save data into listingData
server.listen(port); // listen on port
});