Back to results for “ES Modules”
Node.js › Express & Middleware

What is Express.js, and what problem does it solve compared to using Node.js's raw http module directly?

Beginner

Answer

Express is a minimal, unopinionated web framework built on top of Node's core http module, providing routing (mapping URLs and HTTP methods to handler functions), middleware composition, request/response helper methods (like res.json(), req.params), and template-engine integration -- all of which would otherwise need to be implemented manually and repetitively using the lower-level, more verbose raw http module.
// Raw http module: verbose, manual routing
const http = require('node:http');
http.createServer((req, res) => {
  if (req.url === '/users' && req.method === 'GET') { res.end(JSON.stringify(users)); }
}).listen(3000);

// Express: concise, declarative routing
const express = require('express');
const app = express();
app.get('/users', (req, res) => res.json(users));
app.listen(3000);
Real-world example A team building a REST API chooses Express over the raw http module specifically for its concise routing syntax and rich middleware ecosystem (body parsing, CORS, authentication), avoiding having to hand-roll all of that boilerplate themselves.

Common follow-ups: In what scenario might a team deliberately avoid Express and use the raw http module directly instead?;How does Express compare to newer, similarly-purposed frameworks like Fastify or Koa?