How to build an environment for Express.js with core modules, CLI and Docker

I will show you how to build an environment for Express.js. The code is also posted on Git Hub, so please refer to it.

overview

I will explain various environment construction methods of Express.js, which is a Node.js-based web framework.

In this article, we will introduce how to build the environment in the following three ways.


  1. How to build environment with Express.js core module
  2. How to build an environment using Express Generator
  3. How to build an environment using Docker

This section describes how to build an Express.js environment using the following versions.

Express.js v4.18.1
nodejs v19.7.0

In addition, all the code created this time is posted on GitHub.

*** Express.js works with Node.js. If Node.js is not installed, please download it from the official website and install it. ** node.js

How to build an environment using core modules

This method requires and uses the core modules of Express.js. Please follow the steps below.

To build an environment with Express.js alone, the following prerequisites must be met.

Create project directory

First, create a new directory to create your Express.js project. Run the below command in command line

mkdir express-core-project

Change to project directory

Go to your project directory. Please run the following command:

cd express-core-project

After moving, move to the root directory of the project.

Project initialization

Next, initialize the project in the created directory

become Create a package.json file by running the following command

npm init -y

This command will generate package.json with default settings.

Installing Express.js

Install the Express.js package. Please run the below command

npm install express@4.18.1

This will add Express.js to your project.

Creating an application

Create an Express.js application. Create a new file (e.g. app.js) inside your project directory and add the following code

app.js

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Express.js!');
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

The example above shows the basic structure of an Express.js application. Define a handler for the root path using the app.get() method and return Hello, Express.js! as a response.

The final directory structure will look like this:

express-core-project
├── node_modules
│ └── (dependency package)
├── app.js
├── package-lock.json
└── package.json

Start the server

Finally, start the Express.js server. Please run the below command

node app.js

If the server starts successfully, you should see “Server started on port 3000” in the console. Visit http://localhost:3000 in your browser to make sure your Express.js application is working properly.

How to build an environment using Express Generator

Express.js can also build environments using the Express Generator. Express Generator is a command line tool for easily creating Express.js projects. In this article, I will explain in detail the environment construction procedure using Express Generator.

Install Express Generator

Install Express Generator globally. Please run the following command:

npm install -g express-generator

This will install the Express Generator on your system.

Creating a project

Run the following command to create an Express.js project:

express express-generator-project

The above command will create a project directory named express-generator-project. You can change the directory name to anything you like.

Change to project directory

Go to your project directory. Please run the following command:

cd express-generator-project

After moving, move to the root directory of the project.

Installing dependencies

In your project root directory, install the dependencies: Please run the following command:

npm install

This will add Express.js and related packages to your project. The final directory structure will look like this:

express-generator-project
├── node_modules
│ └── (dependency package)
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
├── views
│ ├── error.jade
│ ├── index.jade
│ └── layout.jade
├── app.js
├── package.json
└── package-lock.json

Start the server

Start the Express.js server. Please run the following command:

npm start

If the server starts successfully, you should see “Server started on port 3000” in the console. Visit http://localhost:3000 in your browser to make sure your Express.js application is working properly.

Using the Express Generator makes it easy to build an environment for Express.js projects. Just install the Express Generator, create a project using the express command, install the dependencies and you can quickly start developing Express.js applications.

How to build an environment using Docker

First of all, install Docker. Install Docker for your supported operating system from the official Docker website

Please download the installer and install it.

Create project directory

First, create a new directory to create your Express.js project. Run the below command in command line

mkdir express-docker-project

Change to project directory

Go to your project directory. Please run the following command:

cd express-docker-project

After moving, move to the root directory of the project.

Project initialization

Run the following command to initialize the Express.js project.

npm init -y

Install Required Packages

Install Express.js and the packages required to build Docker images.

npm install express@4.18.1

Creating a Dockerfile

To build your Express.js application as a Docker image, create a file named Dockerfile in your project folder and add the following content.

FROM node:19.7.0-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY..

EXPOSE 3000

CMD ["node", "app.js"]

Creating an application

Create an Express.js application. Create a new file (e.g. app.js) inside your project directory and add the following code

Docker listens on 0.0.0.0 to allow connections from all hosts.

app.js

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Express.js!');
});

app.listen(3000, '0.0.0.0', () => {
  console.log('Server started on port 3000');
});

Building a Docker image

Build the Docker image using the command below.

docker build -t express-docker-project .

Running a Docker container

Run a Docker container from the built Docker image.

docker run -p 3000:3000 -d express-docker-project

This will run your Express.js application inside a Docker container and map it to port 3000 on your host. You can access your Express.js application by visiting http://localhost:3000 from your browser, API test tool, etc.

The above is the procedure for building an environment for an Express.js application using Docker. By using Docker, you can reduce the work of building the environment and make the development process smoother.

Note: Access to port 3000 may be restricted by the host’s firewall or network settings. Adjust your firewall and network settings as needed.