diff --git a/README.md b/README.md index 028bbcd0..813e20b1 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,8 @@ Some resources you may find useful: - The File System's [readFile() method](https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback) - [Different MIME Types/File types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types) +### Test Result +![alt text](./screenshot.png "test result") diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 00000000..bd21e59b Binary files /dev/null and b/screenshot.png differ diff --git a/server.js b/server.js index 46904d26..dd2571c6 100644 --- a/server.js +++ b/server.js @@ -1,26 +1,46 @@ var http = require('http'), - fs = require('fs'), - url = require('url'), - port = 8080; +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 (parsedUrl.pathname == '/listings'){ + response.writeHead(200, {'Content-Type': 'application/json'}); + response.write(listingData); + } + else{ + response.writeHead(404, {'Content-Type': 'text/plain'}); + response.write('Bad gateway error'); + } + response.end(); /* - 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 - */ + 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 + */ }; +server = http.createServer(requestHandler); +console.log("Created Server..."); fs.readFile('listings.json', 'utf8', function(err, data) { /* - This callback function should save the data in the listingData variable, - then start the server. - */ + This callback function should save the data in the listingData variable, + then start the server. + */ + if (err){ + console.log(err); + return; + } + else{ + listingData = data; + server.listen(port); + } }); +console.log("Test..."); \ No newline at end of file