Simple web server with Express in Node.js

This page explains how to create a very simple web server with Express in Node.js.

Express is a web application framework for Node.js. It is designed for building web applications and APIs. It provides a simple and flexible way to handle routing, middleware, and other request-response functionality. Express makes it easy to handle HTTP requests and responses and allows developers to build robust and scalable web applications. It is widely used and well-documented, with a large and active developer community.

The following has been tested with theses versions:

The following assumes Node.js et npm are installed. Read this page, if you have to install Node.js on Ubuntu.

Workspace

Create a new folder for your project and go inside. Let's start by creating the Node.js project:

npm init -y

Install the Express module.

npm install express --save

The --save flag is used to add Express to your package.json file In another words, it add express as a dependency. After the installation is complete, open up your package.json to see express listed as a dependency:

"dependencies": {
    "express": "^4.18.2"
}

When deploying your project on another machine, just run npm install to install all the dependencies.

Creating the server

Create a file named server.js and copy the following code:

// Import the express module
var express = require("express");

// Initialize express
var app = express();

The first line imports the express module. The second one initialize Express.

Now, let's create an event listener. When the home page of our server is requested, a callback function is executed. Append the following code to server.js:

app.get("/", (req, res, next) => {
    res.set('Content-Type', 'text/html');
    res.send('<html><body><h1>Home Page</h1></body></html>');
});

The first line create an event when the home page is requested (/).

We set the response type text/html. And then send to the client an raw HTML code:

<html><body><h1>Home Page</h1></body></html>

Finally, we can start the server on port 3000 with the following code:

// The server start listening on port 3000
app.listen(3000, () => {
    console.log("Server running on port 3000");
});

Run the server

Start the server with the following command:

node server.js

Go to your browser and check the URL http://localhost:3000:

Home page the Express web server created with Node.js

New page

You can easily create a new page by adding new routes. For example, this event will be triggered when the user requests the path /api/:

app.get("/", (req, res, next) => {
    res.set('Content-Type', 'text/html');
    res.send('<html><body><h1>Home Page</h1></body></html>');
});

Note that here, we serve json content to the client at the address http://localhost:3000/api

Serve JSON content on http://localhost:3000/api

Full code

Here is the full code of our server:

// Import the express module
var express = require("express");

// Initialize express
var app = express();

// Home page
app.get("/", (req, res, next) => {
    res.set('Content-Type', 'text/html');
    res.send('<html><body><h1>Home Page</h1></body></html>');
});

// On path /api/ we serve JSON content
app.get("/api", (req, res, next) => {
    res.set('Content-Type', 'application/json');
    res.send('{"name":"John","age":30,"cars":["Ford", "BMW", "Fiat"]}');
});

// The server start listening on port 3000
app.listen(3000, () => {
    console.log("Server running on port 3000");
});

Download

You can download the Express Web server source code below. To run the server, unzip the file and run the following lines in the project folder:

npm install
npm start

nodejs-express-web-server.zip

See also


Last update : 01/12/2023