-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (45 loc) · 2 KB
/
server.js
File metadata and controls
55 lines (45 loc) · 2 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
var http = require('http'), /* require/import the HTTP module */
fs = require('fs'),
url = require('url'),
port = 8080; /* define a port we want to listen to */
/* Global variables */
var listingData, server;
var requestHandler = function(request, response) {
var parsedUrl = url.parse(request.url); //requesting and responding the complete URL
/*
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
*/
var myJSON;
if(parsedUrl.pathname == '/listings'){ // pathway is listings. pathway is the resource path part of the URL
myJSON = JSON.stringify(listingData); // data must be a string when sending data to a web server. Stringify converts Javascript object into a string
response.end(myJSON); // print the statement on web
}
else{
response.writeHead(404);
response.end('Bad gateway error');
}
};
fs.readFile('listings.json', 'utf8', function(err, data) {
/*
This callback function should save the data in the listingData variable,
then start the server.
*/
listingData = JSON.parse(data) //if you parse the data with json.parse(), it becomes a javascript object
// a server is created, but not started
var server = http.createServer(requestHandler);
// the server is now started, listening for requests on port 8080
server.listen(port, function() {
//once the server is listening, this callback function is executed
console.log('Server listening on: http://127.0.0.1:' + port); //print statement on terminal
});
});
//console.log('Is the server started?');
/* If error encountered, print error path. Else print stataus code 200 OK */
//request handler
// function createRequestHandle(request) {
// body...
// if ()
// }