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:
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
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
| Feature | Description |
|---|---|
| Routing | Convenient request routing (app.get('/path', ...)) |
| Middleware | Functions that process a request before/after a route |
| Working with JSON | Automatic request-body parsing |
| Template engines | Support for ejs, pug, handlebars, and others |
| Static files | Simple file serving (app.use(express.static('public'))) |
| Error handling | Centralized error handling |
| Integration | Compatible 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:
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next(); // pass control onward
});Every middleware can:
- change
reqorres; - 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:
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
| Area | Application |
|---|---|
| REST API | Backends for SPAs and mobile apps |
| SSR | Server-side rendering of React/Vue/Nunjucks |
| Middleware chains | Authentication, logging, error handling |
| Microservices | Lightweight microservices with a JSON API |
| Backend-for-Frontend | A 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 readyA concise answer to help you respond confidently on this topic during an interview.