Introduction to Node.js

Welcome to my MDX page!

What is Node.js?

Node.js is a JavaScript runtime that lets you run JavaScript outside the browser. It is commonly used to build web servers, APIs, command-line tools, and real-time applications.

Creating a simple server

Node.js includes the built-in http module, which can handle requests without installing additional packages:

const http = require("node:http");

const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "text/plain" });
  response.end("Hello from Node.js!");
});

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

Save the code in server.js, then run it with:

node server.js

Open http://localhost:3000 in a browser to see the response.

Why use Node.js?

  • It uses JavaScript on both the frontend and backend.
  • Its asynchronous model is useful for handling many requests.
  • npm provides a large ecosystem of reusable packages.

Node.js is a practical starting point for learning backend development and building small services.

Built by Rahul S