Fast to start
A working server can fit in a few lines.

A phone loads a product list. A checkout page saves an order. A dashboard asks the server for fresh numbers. Something has to receive those HTTP requests and send an answer back. Express.js is the framework many Node.js developers use to write that answer.
Express is a small, flexible web framework for Node.js. It sits on top of Node’s HTTP tools and gives you routing, middleware, and response helpers. You still write JavaScript. Node still runs it. Express just removes a lot of the repetitive server code.
People call Express fast, unopinionated, and minimal. In everyday terms:
A working server can fit in a few lines.
Express does not choose your database, folder layout, or front end. You do.
The core stays small. Extra behavior arrives as middleware when you need it.
JavaScript = the language you write
Node.js = the runtime that executes it
Express.js = the web framework on top of Node
Create the app → const app = express()
Add a route → app.get('/', handler)
Start listening → app.listen(3000) Framework, runtime, language. Express is a framework. You write it in JavaScript, and Node.js runs it. Install the package in a Node project when you want a website or an API.
Node can already speak HTTP. Express earns its place by making routes, shared request logic, and responses easier to read and reuse.
Skip manual URL checks and header setup. app.get() and res.send() cover the common path.
Each URL and HTTP method gets a function. A file reads like a map of your API.
Logging, JSON parsing, and login checks run before the route, and every route can reuse them.
npm has packages for CORS, cookies, uploads, security headers, and almost any HTTP job.
If you can write a JavaScript function, you can write a route. That makes Express a common first backend.
Express adds tools without hiding Node. Streams, the http server, and npm still work the same way.
The core is intentionally small. These six pieces show up in almost every Express app.
GET, POST, PUT, PATCH, and DELETE. Read :id from the path and values from the query string.next() to continue, or end the response early.req holds params, query, headers, and body. res sends text, JSON, files, redirects, and status codes.express.static() serves CSS, images, and browser JavaScript from a folder such as public.res.render().express.Router() groups related routes. Mount that mini-app at a prefix like /api.This handler answers GET /books/42. Express fills req.params.id from the :id segment, then res.json() sends data the client can parse.
app.get('/books/:id', (req, res) => {
res.json({
id: req.params.id,
title: 'A brief history of APIs'
});
}); Query strings too. A request to /search?q=express exposes the word through req.query.q. Path params and query params are two different places to read input.
Every request walks a line of functions. The first matching route does the work, unless an earlier function already sent a response.
A browser, mobile app, or curl calls a URL on your server.
Each function can log, parse, or check the request, then call next().
Express compares the HTTP method and the path, then runs that handler.
res.send(), res.json(), or res.render() finishes the round trip.
Express continues down the stack. You can add your own 404 handler at the end.
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log(req.method, req.path);
next();
});
app.get('/hello/:name', (req, res) => {
res.send('Hello, ' + req.params.name);
});
app.listen(3000); Open http://localhost:3000/hello/Ada and the terminal prints GET /hello/Ada. The browser shows Hello, Ada. The logger runs for every path because it was registered with app.use(). The greeting runs only when the method and path match.
Always continue or reply. If middleware neither calls next() nor sends a response, the browser waits until the request times out. That hanging request is one of the first bugs beginners hit.
When a later function throws, Express can catch it in error-handling middleware. That function lists the error first: (err, req, res, next). It is the same pipeline, with a place to send a calm error response.
Express fits HTTP work of many sizes. The pattern stays the same: match a route, run the logic, send the right response.
Return books, users, or orders with res.json() for other programs to consume.
Power a React, Vue, or mobile client. The screen lives on the device. Express handles the data.
Build HTML on the server with EJS, Pug, or another view engine. This tutorial site does that.
Accept a POST when a payment, a git push, or a form event happens somewhere else.
Run one focused HTTP service that other parts of your system call over the network.
Serve images from disk, or send a generated file with res.download().
Picture a small bookstore. The mobile app calls GET /api/books and gets JSON. Checkout sends POST /api/orders. A payment provider later calls POST /webhooks/payment when the charge succeeds. The staff page renders HTML from a template. Those routes can live in one Express app, or you can split them into smaller services later.
Bring your own database. Express decides which function runs. A library such as a MongoDB driver, and the code you write, decide what to save.
Express runs inside Node.js. Install it in a Node project, and Node still executes your JavaScript, loads npm packages, and runs the event loop.
| Topic | Node.js | Express.js |
|---|---|---|
| What it is | A JavaScript runtime for servers, tools, and scripts | A web framework package that runs on Node |
| How you get it | Install Node from nodejs.org. npm comes with it | npm install express inside a project |
| HTTP server | http.createServer() and server.listen() | express() and app.listen() |
| Routing | You inspect req.method and req.url yourself | app.get('/books/:id', handler) matches for you |
| Shared logic | You invent a way to reuse request code | Middleware plus next() is built in |
| Responses | writeHead() and end() with your own headers | res.send(), res.json(), res.status() |
| Reach for it when | You need the runtime, files, processes, or a tiny script | You are building URLs, APIs, or HTML over HTTP |
Both programs answer GET /. The Node version spells out the status and headers. The Express version names the route and lets res.send() finish the response.
const http = require('node:http');
const server = http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Hello from Node');
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Not found');
});
server.listen(3000); const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.type('txt').send('Hello from Express');
});
app.listen(3000); A string to res.send() is treated as HTML unless you set another type. res.type('txt') marks this greeting as plain text. For data, prefer res.json().
This page is the map. The next lessons open each part of an Express app, one object at a time.
app, register routes, change settings, and start the server with listen. express.json(), express.urlencoded(), express.static(), and express.Router(). req. res. You can learn Express while you build the first app. A few skills make that afternoon much smoother.
node file.js, start a project, and install a package. Start with the Node.js introduction if that is new.node -v.GET reads and POST sends data, and that port 3000 is just a door number on your machine.Install Node first. Download the current LTS release from nodejs.org if node -v fails or prints a version older than 18. npm is included.
Build a tiny notes API: a welcome page, a health check, and one route that accepts a note title as JSON.
npm init -y writes a package.json with the defaults so you can move straight to the install.
mkdir notes-api
cd notes-api
npm init -y
npm install express Save this as app.js in that folder.
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Welcome to your first Express app');
});
app.get('/api/health', (req, res) => {
res.json({ ok: true, service: 'notes-api' });
});
app.post('/api/notes', (req, res) => {
const title = req.body.title || 'Untitled';
res.status(201).json({ saved: true, title: title });
});
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`);
}); node app.js
curl http://localhost:3000/api/health
curl -X POST http://localhost:3000/api/notes -H "Content-Type: application/json" -d "{\"title\":\"Buy milk\"}" Open http://localhost:3000 for the welcome sentence and http://localhost:3000/api/health for the JSON check. In PowerShell, call curl.exe and wrap the JSON in single quotes: -d '{"title":"Buy milk"}'.
express()express.json()req.body. Skip it, and title never arrives.app.get('/')res.json()res.status(201)json() writes the body. Status 201 means “created.”app.listen(3000)Broken JSON never reaches your route. If the client posts invalid JSON, express.json() responds with status 400 before /api/notes runs.
Using import? Add "type": "module" to package.json, then write import express from 'express'. The routes stay the same. This tutorial uses require so the file runs without that setting.
next() or send a response.A useful Express server is three moves: create the app with express(), add a route with app.get(), and start it with app.listen(). Databases, logins, and templates plug in only when the app needs them.
See how the app object registers routes, settings, and middleware.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
12 people found this page helpful