Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test commit #5

Open
wants to merge 2 commits into
base: Assignment1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 32 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
@@ -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...");