What is 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}`));
Learn Express
Learn how to make back-end apps and APIs using the popular Express.js frameworkTry it for freeMIDDLEWARE
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 neededrouter.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!')});
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Back-End Web Architecture
This article provides an overview of servers, databases, routing, and anything else that happens between when a client makes a request and receives a response. - Article
How to Implement MVC Architecture in a Full-Stack App
Learn how to implement MVC architecture in a full-stack expense tracker app using JavaScript, Node.js, Express, React, and PostgreSQL. - Article
How to Request Webpages Using Python
Learn how to request webpages and get JSON data using Python's requests library. A step-by-step guide with practical examples for making HTTP GET and POST requests in Python.
Learn more on Codecademy
- Course
Learn Express
Learn how to make back-end apps and APIs using the popular Express.js frameworkWith CertificateIntermediate5 hours - Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn Node.js: Setting Up a Server
Build an HTTP server using Node.js to facilitate the connection between a client and a server.Beginner Friendly2 hours