Express.js for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Express.js and why is it needed?



Express.js is a minimal and flexible web framework for Node.js, written in JavaScript. It provides developers with a powerful set of tools for creating web applications and APIs (REST, GraphQL). Express.js is the de facto standard for server-side development on Node.js due to its simplicity, speed, and huge community.



Why is Express.js needed? Node.js itself provides a low-level HTTP module that allows you to create servers. However, working with it directly requires writing a lot of boilerplate code for handling routes, parsing requests, managing sessions, etc. Express.js solves these problems by providing:

  • Routing: Simple definition of handlers for different URLs and HTTP methods (GET, POST, PUT, DELETE).
  • Middleware: A powerful system that allows you to execute code before or after processing a request (e.g., for logging, authentication, compression).
  • Request and Response Management: Convenient methods for working with URL parameters, request body, cookies, headers, and sending responses (JSON, HTML, files).
  • Templating: Integration with popular template engines such as Pug, EJS, Handlebars.
  • Static Files: Simple serving of static resources (CSS, JS, images).

Express.js is used to create everything from simple single-page applications to complex enterprise systems and high-load APIs.



Installing Express.js

Installing Express.js is done via the npm (Node Package Manager) package manager. It is assumed that you already have Node.js and npm installed.



Step 1: Initialize the project

Create a new directory for your project and navigate to it in the terminal. Then initialize a new Node.js project:

mkdir my-express-appcd my-express-appnpm init -y

The -y flag automatically accepts all default values. You can also omit it to manually fill in the project metadata.



Step 2: Install Express

Install Express.js as a project dependency:

npm install express

This command will add Express to the package.json file in the dependencies section and create a node_modules folder with the library itself and its dependencies.



Step 3: Verify installation

After installation, you can check the version of Express in your project:

npx express --version

Or simply look in the package.json file.



Quick Start: "Hello World" on Express.js

Create a file app.js (or index.js) in the root of your project and add the following code:



// app.jsconst express = require('express');const app = express();const port = 3000;

// Define a route for the root URLapp.get('/', (req, res) => { res.send('Hello World! Express.js is working!');});

// Start the serverapp.listen(port, () => { console.log(`Server running at http://localhost:${port}`);});


Running the application:

In the terminal, run the command:

node app.js

Open your browser and go to http://localhost:3000. You should see the message "Hello World! Express.js is working!".



What is happening here?

  1. require('express') — import the Express library.
  2. express() — create an instance of the Express application.
  3. app.get('/', callback)

Recommendations