What is Express.js?

Codecademy Team
Learn about the popular web framework Express.js.

Express.js is the most popular web framework for Node.js. It is designed for building web applications and APIs and has been called the de facto standard server framework for Node.js.

Building a backend from-scratch for an application in Node.js can be tedious and time consuming. From setting up ports to route handlers, writing all of the boilerplate code takes away from what really matters, which is writing the business logic for an application. By using web frameworks like Express.js, developers can save time and focus on other important tasks. To learn more about Express and what it can do, visit the official website.

HANDLING REQUESTS

In traditional web applications, a web server will wait for HTTP requests to be sent from the client. Upon receiving a HTTP request, the server will choose the corresponding route handler and delegate further action to it for that request. Normally, writing a route handler from scratch can be a bit burdensome in Node. Fortunately, Express provides methods to specify what function is called for a particular HTTP verb (GET, POST, PUT, etc.) and URL pattern (Route). An example of a Express route is demonstrated in the code snippet below. Here, Express is declaring that all GET requests made to the route, /, will be handled with a function that responds to the client with “Hello World!”

const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

MIDDLEWARE

Express is an un-opinionated framework, meaning that it allows developers the freedom to structure their code how they choose instead of forcing a particular code structure. One place where this un-opinionated stance can be seen is with the application of middleware. Middleware allows for operations to be performed on requests and responses moving through the routes and is extensively used in Express apps. Middleware can be applied at both the application and route levels as well as be chained together. You can insert almost any compatible middleware you like into the request handling chain, in almost any order you like.

A typical Express route will usually consist of some middleware and a route handler function. The following example shows an Express router that applies middleware to every HTTP GET request made to the /user/:id route. The request and response pass through the middleware and ultimately arrive at the route handler function, which ends the HTTP request-response cycle by returning “hello, user!” to the HTTP client.

var express = require('express');
var app = express();
var router = express.Router();
// predicate the router with a check and bail out when needed
router.use(function (req, res, next) {
if (!req.headers['x-auth']) return next('router')
next()
});
router.get('/user/:id', function (req, res) {
res.send('hello, user!')
});