Node.js Web Framework
Express (also called Express.js) is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It's designed to make the process of building web applications and APIs with Node.js much simpler and faster.
Released in 2010, Express has become the de facto standard server framework for Node.js, offering a thin layer of fundamental web application features without obscuring Node.js features. It's known for its performance and minimalist approach, allowing developers to add only what they need.
const express = require('express'); const app = express(); const port = 3000; // Define a route handler for GET requests to the root path app.get('/', (req, res) => { res.send('Hello, Express!'); }); // Start the server app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });
const express = require('express'); const app = express(); // Custom middleware function const logger = (req, res, next) => { console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`); next(); // Pass control to the next middleware }; // Apply middleware to all routes app.use(logger); // Parse JSON request bodies app.use(express.json()); // Route with URL parameters app.get('/users/:id', (req, res) => { const userId = req.params.id; res.json({ message: `Fetching user with ID: ${userId}`, user: { id: userId, name: 'Example User' } }); });
const express = require('express'); const router = express.Router(); // GET all items router.get('/', (req, res) => { res.json({ message: 'Retrieving all items' }); }); // GET a specific item router.get('/:id', (req, res) => { res.json({ message: `Retrieving item ${req.params.id}` }); }); // POST a new item router.post('/', (req, res) => { res.status(201).json({ message: 'Item created', item: req.body }); }); // PUT (update) an item router.put('/:id', (req, res) => { res.json({ message: `Updating item ${req.params.id}`, item: req.body }); }); // DELETE an item router.delete('/:id', (req, res) => { res.json({ message: `Deleting item ${req.params.id}` }); }); // Register the router with a base path app.use('/api/items', router);
Express is primarily used for:
Major milestones in Express's development:
While Express hasn't seen major version updates recently, it remains the most widely used Node.js framework due to its stability, simplicity, and vast ecosystem.
Here are some excellent resources for learning Express:
Technologies often used with Express or alternative options: