Express is a minimal, unopinionated, flexible Node.js web application framework. It’s widely used due to its simplicity, speed, and scalability.

Key Features

  • Middleware: Express is built around “middleware”. Middleware functions have access to the request object (req)m the response object (res), and the next middle ware function. This makes the app more extensible and maintainable.
  • Routing: Express uses the app.get(), app.post(), app.put(), and app.delete() methods to define routes, making it easy to build RESTful APIs and web apps.
  • HTTP Utility Methods: HTTP utility methods for handling headers, cookies, query params, sending responses, and managing requests.

Express.js Hello World

This app starts a server and listens on port 3000 for connections. The app responds with “Hello world!” for request to the route URL.

npm install express
// app.js
import express from 'express';
 
const app = express(); // create an express application
const port = 3000;
 
// define a route at the root path
app.get('/', (req, res) => {
  res.send('Hello World!');
});
 
// start up the server at specified port
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Start the server, then navigate to http://localhost:3000 to see “Hello World!” displayed.

node app.js

Route handlers

Routes allow you to match particular patterns of characters in a URL, extract some values, and pass them as parameters to the route handler.

express.Router

express.Router allows you to create modular, mountable route handlers. Routers can help organize your routes into separate modules.

It’s often used to group route handlers for a particular part of a site and access them using a common route-prefix. For example, we can group routes for a website wiki:

// wiki.js - Wiki route module
 
import express from 'express';
 
const router = express.Router();
 
// Home page route
router.get("/", function (req, res) {
  res.send("Wiki home page");
});
 
// About page route
router.get("/about", function (req, res) {
  res.send("About this wiki");
});
 
module.exports = router;

To use the router in our main app file we would then import the route module and call use() to add the Router to the middleware handling path.

import wiki from './wiki.js';
// ...
app.use("/wiki", wiki);

Middleware

Middleware is used extensively in Express apps. Middleware functions typically perform some operation on the request or response and then call the next function.

Most apps use third-party middleware to simplify common web dev tasks like working with cookies, sessions, user authentication, logging, etc.

You can add a middleware function for all responses, for a specific route, or for a specific HTTP verb + route.

import express from 'express';
const app = express();
 
const a_middleware_function = function (req, res, next) {
  // Perform some operations...
  next(); // Call next() so Express will call the next middleware function
};
 
// Function added with use() for all routes and verbs
app.use(a_middleware_function);
 
// Function added with use() for a specific route
app.use("/someroute", a_middleware_function);
 
// A middleware function added for a specific HTTP verb + route
app.get("/", a_middleware_function);
 
app.listen(3000);