Question: Below is my code to the assignment that will be attached beow, I am unable to detect or connect to the localhost:3000. I am not
Below is my code to the assignment that will be attached beow, I am unable to detect or connect to the localhost:3000. I am not sure if my code is correct or not... please help
One thing to note is The server can only use Node's built-in modules (e.g. http, fs, path, etc.), no third-party modules.
//////////////////////////////////////////////////////////////////////////-------------------MY CODE-------------------////////////////////////////////////////////////////////////////////////////////////////////////
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, resp)
{
//check for the /index.html
if (req.url === "/index.html")
{
fs.readFile("d:\\public\\index.html", function (error, pgResp3)
{
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp3);
resp.end();
});
server.listen(3000);
//console.log is used to send message in server console
console.log('Server Started listening on 3000');
console.log('index.html');
}
else
//check for the /index.js
if (req.url === "/index.js")
{
//pgResp1 is used so the file will not be called again
fs.readFile("d:\\public\\index.js", function (error, pgResp1)
{
//status code 200
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp1);
resp.end();
});
console.log('index.js');
}
else
//check for the /style.css
if (req.url === "/style.css")
{
fs.readFile("d:\\public\\style.css", function (error, pgResp2)
{
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp2);
resp.end();
});
console.log('style.css');
}
//check for if any other URL then 404.html will be send
else
{
fs.readFile("d:\\public\\404.html", function (error, pgResp)
{
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp);
resp.end();
});
}
});
//////////////////////////////////////////////////////////////////////////------------------Assignment discription-------------------////////////////////////////////////////////////////////////////////////////////////////////////
The server should listen for requests on the port specified by the environment variable PORT. If PORT is not present in the environment, the server should listen on port 3000 by default.
When someone requests a path from your server that corresponds to the name of one of the files in public/, your server should respond with the contents of that file and a status code of 200. For example, if you run your server on port 3000 on your laptop, you should be able to access the following files by entering the following URLs into your browser:
public/index.html - http://localhost:3000/index.html
public/index.js - http://localhost:3000/index.js
public/style.css - http://localhost:3000/style.css
public/404.html - http://localhost:3000/404.html
Note that if everything is hooked up correctly, your index.html and 404.html pages will automatically have styles and interactions from style.css and index.js because the browser will see those files referenced from the HTML and make additional requests for those files.
When someone requests the root path (i.e. /) from your server, it should respond with the contents of public/index.html and a status code of 200. For example, if you run your server on port 3000 on your laptop and visit http://localhost:3000 in your laptop's browser, your server should send the contents of public/index.html.
If someone visits a path on your site that does not correspond to the name of any of the files in public/, your server should respond with the contents of public/404.html and a status code of 404. For example, if you run your server on port 3000 on your laptop and visit http://localhost:3000/thispagedoesnotexist in your laptop's browser, your server should serve the contents of public/404.html.
Your server should read any given file in public/ from disk only once. In other words, the contents of each file should be cached in the server's memory after the first read, and the server should use this cache when responding with a file's contents instead of reading the file a second time. You should add a console.log() statement immediately before each call to read a file to prove to yourself that each file is being read only once.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
