"Unhandled Error" message while setting up server through http module in node js

Solution 1:

This will solve your problem, you need to have all the code wrapped in if else statements so that it can always terminate successfully with only one possible path through the code.

const http = require("http");
const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.end("Welcome to our home page");
  } else if (req.url === "/aboutus") {
    res.end("Welcome to about us page");
  } else {
    res.end(`
        <h1> OOPS !!! </h1>
        <p>You have requested for any wrong address </p>
        <a href = "/">Get back to HOME Page</a>
    `);
  }
});
server.listen(5000);

Personally, I would use a switch case statement for this to keep it nice and clean.

const http = require("http");
const server = http.createServer((req, res) => {
  switch (req.url) {
    case "/":
      res.end("Welcome to our home page");
      break;

    case "/aboutus":
      res.end("Welcome to about us page");
      break;

    default:
      res.end(`
        <h1> OOPS !!! </h1>
        <p>You have requested for any wrong address </p>
        <a href = "/">Get back to HOME Page</a>
      `);
      break;
  }
});
server.listen(5000);