{ console.log(req); // Response we Send Back res.end("Hello from the server!"); }); // Listening for Incoming Requests on the Local Host on Port 8000 server.listen(8000, "127.0.0.1", () => { console.log("Listening to requests on port 8000"); });"> { console.log(req); // Response we Send Back res.end("Hello from the server!"); }); // Listening for Incoming Requests on the Local Host on Port 8000 server.listen(8000, "127.0.0.1", () => { console.log("Listening to requests on port 8000"); });"> { console.log(req); // Response we Send Back res.end("Hello from the server!"); }); // Listening for Incoming Requests on the Local Host on Port 8000 server.listen(8000, "127.0.0.1", () => { console.log("Listening to requests on port 8000"); });">
// Networking Capabilities: HTTP Server
const http = require("http");

// -------------------- Server --------------------
const server = http.createServer((req, res) => {
  console.log(req);
  // Response we Send Back
  res.end("Hello from the server!");
});

// Listening for Incoming Requests on the Local Host on Port 8000
server.listen(8000, "127.0.0.1", () => {
  console.log("Listening to requests on port 8000");
});

Screenshot 2024-01-24 at 10.35.51 PM.png

Routing

Routing means implementing different actions for different URLs.

// -------------------- Server --------------------
const server = http.createServer((req, res) => {
  const pathName = req.url;

  // -------------------- Basic Routing --------------------

  if (pathName === "/" || pathName === "/overview") return res.end("This is the OVERVIEW");
  if (pathName === "/product") return res.end("This is the PRODUCT");
  
  // This has to be Set up before We Send out the Response
  res.writeHead(404, {
    "Content-type": "text/html", // now the browser is expecting html to come in
    "my-own-header": "hello-world",
  });
  return res.end("<h1>Page not found!</h1>");
});

// Listening for Incoming Requests on the Local Host on Port 8000
server.listen(8000, "127.0.0.1", () => {
  console.log("Listening to requests on port 8000");
});

HTTP Header is a piece of information about the response that we are sending back.

Side note: When we are using a browser, the browser automatically performs a request for the website's favicon

These routes that we defined in this code and the routes that we put in the URLs in the browser have nothing to do with files and folders in a project’s file system.

API

An API is a service from which we can request some data.

So, all Node JS scripts they get access to a variable called __dirname, and that variable always translates to the directory in which the script that we are currently executing is located. __dirname: a global variable that represents the directory name of the current module.

"utf-8": a string representing the encoding of the file.