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.
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.
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 (/
).
req
is an object containing information about the HTTP request.res
is the response your function will send back to the client.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");
});
Start the server with the following command:
node server.js
Go to your browser and check the URL http://localhost:3000:
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
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");
});
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