Express.js Introduction

Beginner
10 min read
Updated: Nov 2026
Node.js web framework

What is Express.js?

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:

Fast to start

A working server can fit in a few lines.

Unopinionated

Express does not choose your database, folder layout, or front end. You do.

Minimal

The core stays small. Extra behavior arrives as middleware when you need it.

Remember
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.

Why Use Express.js?

Node can already speak HTTP. Express earns its place by making routes, shared request logic, and responses easier to read and reuse.

Less boilerplate

Skip manual URL checks and header setup. app.get() and res.send() cover the common path.

Routes you can scan

Each URL and HTTP method gets a function. A file reads like a map of your API.

Shared middleware

Logging, JSON parsing, and login checks run before the route, and every route can reuse them.

A large ecosystem

npm has packages for CORS, cookies, uploads, security headers, and almost any HTTP job.

Friendly first server

If you can write a JavaScript function, you can write a route. That makes Express a common first backend.

A thin layer

Express adds tools without hiding Node. Streams, the http server, and npm still work the same way.

Key Features of Express.js

The core is intentionally small. These six pieces show up in almost every Express app.

Routing
Match GET, POST, PUT, PATCH, and DELETE. Read :id from the path and values from the query string.
Middleware
Functions run in order for a request. Call next() to continue, or end the response early.
Request and response
req holds params, query, headers, and body. res sends text, JSON, files, redirects, and status codes.
Static files
express.static() serves CSS, images, and browser JavaScript from a folder such as public.
Views
Render HTML with a template engine such as EJS or Pug. Set the view engine, then call res.render().
Routers
express.Router() groups related routes. Mount that mini-app at a prefix like /api.

A route in one function

This handler answers GET /books/42. Express fills req.params.id from the :id segment, then res.json() sends data the client can parse.

JavaScript
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.

How Express.js Works

Every request walks a line of functions. The first matching route does the work, unless an earlier function already sent a response.

  1. A request arrives

    A browser, mobile app, or curl calls a URL on your server.

  2. Middleware runs in order

    Each function can log, parse, or check the request, then call next().

  3. A route matches

    Express compares the HTTP method and the path, then runs that handler.

  4. You send a response

    res.send(), res.json(), or res.render() finishes the round trip.

  5. Nothing matched?

    Express continues down the stack. You can add your own 404 handler at the end.

JavaScript
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.

What Can You Build with Express.js?

Express fits HTTP work of many sizes. The pattern stays the same: match a route, run the logic, send the right response.

  • JSON APIs

    Return books, users, or orders with res.json() for other programs to consume.

  • App backends

    Power a React, Vue, or mobile client. The screen lives on the device. Express handles the data.

  • Server-rendered sites

    Build HTML on the server with EJS, Pug, or another view engine. This tutorial site does that.

  • Webhooks

    Accept a POST when a payment, a git push, or a form event happens somewhere else.

  • Small services

    Run one focused HTTP service that other parts of your system call over the network.

  • Files and downloads

    Serve images from disk, or send a generated file with res.download().

One bookstore, several routes

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.js vs Node.js

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.

TopicNode.jsExpress.js
What it isA JavaScript runtime for servers, tools, and scriptsA web framework package that runs on Node
How you get itInstall Node from nodejs.org. npm comes with itnpm install express inside a project
HTTP serverhttp.createServer() and server.listen()express() and app.listen()
RoutingYou inspect req.method and req.url yourselfapp.get('/books/:id', handler) matches for you
Shared logicYou invent a way to reuse request codeMiddleware plus next() is built in
ResponseswriteHead() and end() with your own headersres.send(), res.json(), res.status()
Reach for it whenYou need the runtime, files, processes, or a tiny scriptYou are building URLs, APIs, or HTML over HTTP

The same hello, two styles

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.

JavaScript
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);
JavaScript
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().

What You Will Learn in This Tutorial

This page is the map. The next lessons open each part of an Express app, one object at a time.

  1. 01 Application The app object Create app, register routes, change settings, and start the server with listen.
  2. 02 Express module Built-in helpers Use express.json(), express.urlencoded(), express.static(), and express.Router().
  3. 03 Request What the client sent Read params, query strings, headers, cookies, the URL, and the parsed body from req.
  4. 04 Response What you send back Send text and JSON, set status codes, redirect, save cookies, and render a view with res.
  5. 05 Router Routes in modules Split a growing API into files and mount each router on the main app.

Prerequisites

You can learn Express while you build the first app. A few skills make that afternoon much smoother.

  • JavaScript basics. Variables, functions, objects, arrays, and arrow functions. The JavaScript tutorial covers them.
  • Node.js basics. Run node file.js, start a project, and install a package. Start with the Node.js introduction if that is new.
  • A terminal and an editor. You will create a folder, run npm, and edit one JavaScript file.
  • Node.js 18 or newer. Express 5 expects that. Check with node -v.
  • Helpful, not required. Knowing that 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.

Your First Express.js Application

Build a tiny notes API: a welcome page, a health check, and one route that accepts a note title as JSON.

1. Create a project and install Express

npm init -y writes a package.json with the defaults so you can move straight to the install.

terminal
mkdir notes-api
cd notes-api
npm init -y
npm install express

2. Write the server

Save this as app.js in that folder.

JavaScript
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}`);
});

3. Start it and try the routes

terminal
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"}'.

What each part does

express()
Creates the app that will hold middleware and routes.
express.json()
Reads a JSON body and stores it on req.body. Skip it, and title never arrives.
app.get('/')
Sends an HTML string. That is why the browser shows the welcome sentence.
res.json()
Sends an object as JSON and sets the content type for you.
res.status(201)
Marks the note as created, then json() writes the body. Status 201 means “created.”
app.listen(3000)
Opens the server. The backticks drop the port number into the log message.

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.

  • Express is a small Node.js framework for HTTP apps and APIs.
  • Order: create the app, add middleware and routes, then listen.
  • Middleware runs before the handler. Call next() or send a response.
  • Node stays in charge. Express organizes the HTTP layer. You still pick the database and the structure.

Frequently Asked Questions

Express.js is a minimal, flexible web framework for Node.js. It helps you build web apps and APIs with routing, middleware, and helpers for HTTP requests and responses.
No. Express is a framework, and you write it in JavaScript. Node.js is the runtime that executes that JavaScript on a server or your computer.
Node.js runs JavaScript outside the browser and includes a low-level HTTP module. Express.js is a package you add to a Node project so routing, middleware, and responses take less code.
Yes, the basics. You should be comfortable running a file with node, installing packages with npm, and writing everyday JavaScript functions. You do not need to master the raw http module first.
Middleware is a function that runs while a request is being handled. It can log the request, read a JSON body, check a login, or stop the request with an error. Call next() to pass control to the next function.
REST and JSON APIs, backends for mobile apps and single-page sites, server-rendered HTML, webhooks, file downloads, and small HTTP services. Pair it with a database when you need to save data.
Yes. Express is open source under the MIT license. You can use it in personal and commercial projects without a license fee.
This tutorial follows Express 5. Install it with npm install express in a project that uses Node.js 18 or newer.
Continue with the Express application object, then the request and response objects, built-in helpers such as express.json() and express.static(), and routers for splitting routes into files.

Did you know?

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.

Next: Express Application

See how the app object registers routes, settings, and middleware.

Express app →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful