Skip to main content

What is Express.js?

1. What Express.js is

Express.js is a minimalist, flexible web framework for Node.js that simplifies building server applications, REST APIs, and web services.

It provides a convenient interface for:

  • handling HTTP requests and responses;
  • routing;
  • working with middleware (in-between handlers);
  • and integrating with databases, templates, JSON, and more.

2. Why Express came about

Without Express, plain Node.js looks something like this:

javascript
const http = require('http'); const server = http.createServer((req, res) => { if (req.url === '/users' && req.method === 'GET') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify([{ name: 'Tim' }])); } else { res.writeHead(404); res.end('Not Found'); } }); server.listen(3000);

The problems:

  • routes have to be handled manually (req.url, req.method);
  • there's no proper middleware system;
  • there's no JSON parsing, body parser, cookies, etc.

Express solves all of that out of the box.

3. A simple Express server example

javascript
const express = require('express'); const app = express(); // Middleware for JSON parsing app.use(express.json()); // A GET route app.get('/', (req, res) => { res.send('Hello, Express!'); }); // A POST route app.post('/users', (req, res) => { res.json({ message: 'User created', data: req.body }); }); // Start the server app.listen(3000, () => console.log('Server running on port 3000'));

Advantages:

  • less code;
  • a readable route structure (app.get, app.post);
  • built-in JSON and middleware support;
  • easy to extend.

4. Express.js's main features

FeatureDescription
RoutingConvenient request routing (app.get('/path', ...))
MiddlewareFunctions that process a request before/after a route
Working with JSONAutomatic request-body parsing
Template enginesSupport for ejs, pug, handlebars, and others
Static filesSimple file serving (app.use(express.static('public')))
Error handlingCentralized error handling
IntegrationCompatible with any DB (MongoDB, PostgreSQL, MySQL, etc.)

5. What Middleware is in Express

Middleware is functions that run between the request and the response.

For example:

javascript
app.use((req, res, next) => { console.log(`${req.method} ${req.url}`); next(); // pass control onward });

Every middleware can:

  • change req or res;
  • stop execution;
  • call next(), to pass control to the next handler.

6. Express as the base for a REST API

Express is a great fit for building RESTful APIs:

javascript
app.get('/api/users', (req, res) => {...}); app.post('/api/users', (req, res) => {...}); app.put('/api/users/:id', (req, res) => {...}); app.delete('/api/users/:id', (req, res) => {...});

That's why Express is the de facto standard for Node.js API backends.

7. Where Express is used

AreaApplication
REST APIBackends for SPAs and mobile apps
SSRServer-side rendering of React/Vue/Nunjucks
Middleware chainsAuthentication, logging, error handling
MicroservicesLightweight microservices with a JSON API
Backend-for-FrontendA layer between the frontend and external APIs

8. Summary

Express.js is a web framework for Node.js that makes building servers and APIs simple, fast, and structured.

It's used for:

  • handling HTTP requests;
  • routing;
  • middleware;
  • building REST APIs;
  • serving static files and templates.

It's the foundation of most Node.js backends (Next.js, NestJS, Sails, Feathers, LoopBack, and more).

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.