Why Port 3000 in Nodejs


Web servers listen for HTTP requests on port 80 and HTTPS requests on port443. You can set a different port, but you must specify it on the URL.

Using the standard ports has drawbacks :

  • They may be in use by other software, such as other web servers or Skype.
  • Linux and macOS block apps listening on ports below 1000 unless they’re launched by a superuser. This grants your script unlimited rights, where it could do anything such as wiping your OS or posting passwords to Twitter. Remember, you’re running your code as well as hidden code inside Node.js and any modules you’ve installed

It’s safer to run web applications with standard permissions on a higher port.Live production servers can use a web server such as NGINX to forwardrequests to Node.js.

Program-2

Write a simple HTTP server created using the Node.js http module and url module add about and contact page.

const http = require("http");
const url = require("url");
 
const server = http.createServer((req, res) => {
  const parsedUrl = url.parse(req.url, true);
 
  if (parsedUrl.pathname === "/") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h1>Hello World!</h1>");
res.end();
  } else if (parsedUrl.pathname === "/about") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h1>About Us</h1>");
res.end();
  } else if (parsedUrl.pathname === "/contact") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h1>Contact Us</h1>");
res.end();
  } else {
res.writeHead(404, { "Content-Type": "text/html" });
res.write("<h1>Page Not Found</h1>");
res.end();
  }
});
 
server.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});
 

Program Explanation :

1. Import the http and url modules:

const http = require("http");
const url = require("url");
 

2. Create an HTTP server using the createServer method of the http module:

const server = http.createServer((req, res) => {
  // Request handler function
});
 

This method takes a request handler function as an argument, which will be called whenever a new request comes in. The function takes two arguments: the req object, which contains information about the incoming request, and the res object, which is used to send the response back to the client.

3. Parse the requested URL using the url.parse method:

const parsedUrl = url.parse(req.url, true);

This method takes the requested URL as a string and returns an object containing information about the URL, such as the protocol, hostname, pathname, and query parameters. The second argument of the method, if set to true, will parse the query string into an object.

4. Check the pathname of the requested URL and send the appropriate response:

if (parsedUrl.pathname === "/") {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<h1>Hello World!</h1>");
res.end();
} else if (parsedUrl.pathname === "/about") {
res.write("<h1>About Us</h1>");
} else if (parsedUrl.pathname === "/contact") {
res.write("<h1>Contact Us</h1>");
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.write("<h1>Page Not Found</h1>");
}
 

This block of code uses a series of if statements to check the pathname of the requested URL and send the appropriate response. If the pathname is /, it sends a 200 OK status code with an HTML message saying "Hello World!". If the pathname is /about, it sends an HTML message saying "About Us". If the pathname is /contact, it sends an HTML message saying "Contact Us". If the pathname doesn't match any of these, it sends a 404 Not Found status code with an HTML message saying "Page Not Found".

5. Check the pathname of the requested URL and send the appropriate response:

server.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});
 

This method takes a port number and a callback function as arguments. The callback function will be called when the server starts listening on the specified port. In this case, it logs a message to the console saying that the server is running on http://localhost:3000.