Introduction
Express is a minimal and flexible web application framework for Node.js. It sits on top of Node's built-in http module and provides a thin layer of features for building web servers and APIs: routing, middleware, request/response helpers, and template engine integration. Instead of manually parsing URLs and handling every edge case with the raw http module, Express gives you a clean, expressive API to define how your server responds to different requests.
Cricket analogy: Express is like a well-drilled fielding captain who sits on top of raw athleticism (http), giving structure — set fields, calls, and strategy — so the team doesn't improvise every single delivery from scratch.
Syntax
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});Explanation
Calling express() creates an application object, conventionally named app. This object exposes methods like app.get(), app.post(), and others to define routes, plus app.use() to register middleware. app.listen() starts an HTTP server bound to a port and delegates incoming requests to the Express application, which then matches them against registered routes.
Cricket analogy: Calling express() is like naming a new team franchise; app.get()/app.post() are like registering specific set plays for different match situations, and app.listen() is like the umpire actually starting the match on that ground.
Example
const express = require('express');
const app = express();
app.get('/about', (req, res) => {
res.status(200).json({ message: 'About this API', version: '1.0.0' });
});
app.listen(4000, () => {
console.log('API listening on port 4000');
});Output
Visiting http://localhost:4000/about in a browser or with a tool like curl returns a JSON response: {"message":"About this API","version":"1.0.0"} with a 200 OK status. The console logs 'API listening on port 4000' once the server starts successfully.
Cricket analogy: It's like checking a scoreboard app and seeing '{message: About this API, version: 1.0.0}' returned cleanly with a 200 status, confirming the system is live, just as the console logs 'listening on port 4000' at startup.
Key Takeaways
- Express is a lightweight framework built on top of Node's http module.
- app.get(), app.post(), etc. define routes for specific HTTP methods and paths.
- app.listen(port, callback) starts the server and begins accepting connections.
- Express simplifies routing, middleware, and response handling compared to raw Node.js.
Practice what you learned
1. What does calling express() return?
2. Which method starts an Express server listening for connections?
3. What is Express built on top of?
4. Which method is used to define a route that responds to GET requests?
Was this page helpful?
You May Also Like
Routing in Express
Understand how Express matches HTTP methods and URL paths to handler functions using routes.
Middleware in Express
Master the Express middleware chain, the next() function, and the special error-handling middleware signature.
Express Application Structure
Learn how to organize a growing Express project into routes, controllers, middleware, and config folders.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics