Road to Javascript Backend

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine outside a web browser.

Jul 09 2022


Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

A Simple Static File Server

One of the simplest beginner backend projects you can create is a static file server. In its simplest form, a static file server will listen for requests and try to match the requested URL to a file on the local filesystem. Here's a minimal example of that in action:

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

http
  .createServer((req, res) => {
    fs.readFile(__dirname + req.url, (err, data) => {
      if (err) {
        res.writeHead(404, { "Content-Type": "text/html" });
        res.end("404: File not found");
      } else {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.end(data);
      }
    });
  })
  .listen(8000);

For more NodeJS snippets peek here.