Node.js with Routing

This program creates a simple HTTP server using Node.js that serves different content based on the URL requested.

Key Features:
Responds with a plain text message for the / route.
Serves an Home.html file when the /Home.html URL is accessed.

Commands…


This content originally appeared on DEV Community and was authored by Sudhanshu Gaikwad

This program creates a simple HTTP server using Node.js that serves different content based on the URL requested.

Key Features:
Responds with a plain text message for the / route.
Serves an Home.html file when the /Home.html URL is accessed.

Commands
1) npm init
2) npm install nodemon

Create file index.js After that looks like this:
index.js

var http = require("http");
var fs = require("fs");
var path = require("path");
var port = 1515;

var app = http.createServer(function (req, res) {
  const url = req.url;

  if (url === "/Home.html" || url === "/") {
    const filePath = path.join(__dirname, "Home.html");

    fs.readFile(filePath, function (err, data) {
      if (err) {
        res.writeHead(404, { "Content-Type": "text/plain" });
        res.write("404 Not Found");
        res.end();
      } else {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.write(data);
        res.end();
      }
    });
  } else {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.write("This is Routing Server in Node JS..!");
    res.end();
  }
});

app.listen(port, () => {
  console.log("Your server is started on ", port);
});

Home.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>This File Run By Node JS</title>
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
      crossorigin="anonymous"
    />
    <script
      src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
      crossorigin="anonymous"
    ></script>
  </head>
  <body>
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
      <div class="container-fluid">
        <a class="navbar-brand" href="javascript:void(0)">Node Js</a>
        <button
          class="navbar-toggler"
          type="button"
          data-bs-toggle="collapse"
          data-bs-target="#mynavbar"
        >
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="mynavbar">
          <ul class="navbar-nav me-auto">
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="javascript:void(0)">Cotact</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <div class="container">
      <div class="row">
        <h1>This Is a Home Page..!</h1>
      </div>
    </div>
  </body>
</html>

Your server will start, and your Home.html created in the folder looks like this.

Image description


This content originally appeared on DEV Community and was authored by Sudhanshu Gaikwad


Print Share Comment Cite Upload Translate Updates
APA

Sudhanshu Gaikwad | Sciencx (2024-07-15T11:19:12+00:00) Node.js with Routing. Retrieved from https://www.scien.cx/2024/07/15/node-js-with-routing/

MLA
" » Node.js with Routing." Sudhanshu Gaikwad | Sciencx - Monday July 15, 2024, https://www.scien.cx/2024/07/15/node-js-with-routing/
HARVARD
Sudhanshu Gaikwad | Sciencx Monday July 15, 2024 » Node.js with Routing., viewed ,<https://www.scien.cx/2024/07/15/node-js-with-routing/>
VANCOUVER
Sudhanshu Gaikwad | Sciencx - » Node.js with Routing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/15/node-js-with-routing/
CHICAGO
" » Node.js with Routing." Sudhanshu Gaikwad | Sciencx - Accessed . https://www.scien.cx/2024/07/15/node-js-with-routing/
IEEE
" » Node.js with Routing." Sudhanshu Gaikwad | Sciencx [Online]. Available: https://www.scien.cx/2024/07/15/node-js-with-routing/. [Accessed: ]
rf:citation
» Node.js with Routing | Sudhanshu Gaikwad | Sciencx | https://www.scien.cx/2024/07/15/node-js-with-routing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.