Node.jsLesson 5 of 13

Your First Node.js Server

A web server is a program that waits for requests and sends back responses. When you type an address into a browser, the browser sends a request to a server somewhere on the internet. The server answers with HTML, images, or data. With Node.js you can build that server using the built-in http module, with no installs needed.

A web server is a program that waits for requests and sends back responses. When you type an address into a browser, the browser sends a request to a server somewhere on the internet. The server answers with HTML, images, or data. With Node.js you can build that server using the built-in http module, with no installs needed.

Your first server is about ten lines long. It answers every request with a short message. The goal is to see the whole loop: start the server, open a browser, get a response. That loop is the heart of every web application you will ever build, from a blog to an online store. Everything you learn later just adds detail around this same pattern.

Ports deserve a quick word. A port is a numbered door on your computer that a program listens on. Port 3000 is a common choice for development. When you visit http://localhost:3000, localhost means your own machine, and 3000 is the door. If the port is busy, you get an error like EADDRINUSE, meaning another program took the door first. Pick a different port and try again.

To run the server, save the code as app.js, then run node app.js in the terminal. The terminal prints a message and then sits quietly. That quiet is a good sign: the server is running and waiting for visitors. Open http://localhost:3000 in your browser to see the reply. To stop the server, press Ctrl+C in the terminal.

How the code works

Every node js server example follows the same shape: create a server, listen on a port, send a response. The http module provides createServer, which takes a callback that runs on every single request. That callback receives a request object, req, and a response object, res. writeHead sets the status code and content type, end sends the body, and listen starts accepting connections on a port.

JS
const http = require("http");

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

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

Run it

Create a folder for this project. Inside it, create a file named app.js and paste the server code. Then open a terminal in that folder and run node app.js. The terminal prints the start message and then stays quiet, which means the server is waiting. Visit http://localhost:3000 and you should see your message. To stop the server, press Ctrl+C.

JS
node app.js

Common mistakes

Two classic beginner errors: forgetting to restart after an edit, and hitting a port conflict. Node.js does not watch your files. Change the code, stop the server with Ctrl+C, and run node app.js again. If the terminal prints EADDRINUSE, another program already owns port 3000. Change the port to 3001, update the address in your browser, and try again.

JS
// Still serving old code? You edited the file but did not restart.
// Stop it with Ctrl+C and run node app.js again.

Syntax

JS
const http = require("http"); // built-in module, no install needed

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello from Node.js"); // send the reply
});

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

Port 3000 in Use? EADDRINUSE

If port 3000 is busy, server crashes with EADDRINUSE — use process.env.PORT || 3000 and handle error.

JS
server.listen(3000).on("error", e=>console.error(e))

Good to know

  • The http module is built into Node.js.
  • listen binds the server to a port.
  • localhost:3000 points at your own machine.
  • Stop the server with Ctrl+C.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground