DEV Community

Uzeyr OZ
Uzeyr OZ

Posted on

Node Express User Micro service

Step 1: Prepare the Project Structure
First, create a new directory for the project and navigate into it:

mkdir users-module
cd users-module

Enter fullscreen mode Exit fullscreen mode

Inside the users-module directory, create a new file called package.json and add the following content:

{
  "name": "users-module",
  "version": "1.0.0",
  "description": "A module for managing users",
  "main": "app.js",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Enter fullscreen mode Exit fullscreen mode

This file contains information about the project, its dependencies, and its entry point (app.js). We've included the express dependency, which we'll use to create the API for our users module.

Next, run npm install to install the dependencies:

npm install

Enter fullscreen mode Exit fullscreen mode

Step 2: Create the API

In this step, we'll create the API for our users module using Express. Create a new file called app.js and add the following content:

const express = require('express')
const app = express()

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

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

Enter fullscreen mode Exit fullscreen mode

This code creates an Express app and defines a single route that responds with "Hello, world!" when the root URL is requested. We've also included a listener that starts the server and logs a message when it's running.

To test the API, run node app.js and open http://localhost:3000 in your browser. You should see the "Hello, world!" message.

Step 3: Containerize the App with Docker
In this step, we'll create a Dockerfile that will define how to build our Docker image. Create a new file called Dockerfile in the project root directory and add the following content:

FROM node:16-alpine

WORKDIR /app

COPY package.json .
RUN npm install

COPY . .

EXPOSE 3000

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

Enter fullscreen mode Exit fullscreen mode

This Dockerfile starts with the official Node.js 16 Alpine image as its base. It sets the working directory to /app, copies the package.json file and runs npm install to install the dependencies. It then copies the rest of the project files and exposes port 3000. Finally, it sets the command to run the app.js file with Node.

Step 4: Build and Run the Docker Image
In this step, we'll use the Dockerfile to build our Docker image and run a container from it.

First, build the image with the following command:

docker build -t users-module .

Enter fullscreen mode Exit fullscreen mode

This command tells Docker to build an image with the tag users-module using the Dockerfile in the current directory.

Once the build is complete, you can run a container from the image using the following command:

docker run -p 3000:3000 users-module

Enter fullscreen mode Exit fullscreen mode

This command tells Docker to run a container from the users-module image and map port 3000 in the container to port 3000 on the host.

To test the container, open http://localhost:3000 in your browser. You should see the "Hello, world!" message.

Conclusion
In this article, we created a "users" module using Node.js and Express, and containerized it using Docker. This is a

Top comments (0)