Skip to content

gopu22/Nodejs-CRUD-API-Docker

Repository files navigation

Nodejs+CRUD-API+Docker

Building a Dockerized API application in NodeJS for Managing Orders Data in PostgreSQL

  • The controllers/orders.js file contains all the functions to execute in order to interact with the database and have the 4 basic functionalities.
  • The models/order.js file will contain the model, in this case a order with an auto-incremented id, a name and an email.
  • The routes/orders.js file sets up routes for a order management API using the Express.js framework. This module exports an Express Router with various CRUD (Create, Read, Update, Delete) routes for orders.
  • The util/database.js file will contain the internal configuration to allow the connection between the Node.js application and the running Postgres instance.
  • The index.js file is the file that will be executed by the docker container.
  • The Dockerfile is used to build a docker image automatically by reading instructions from the Dockerfile.
  • The Docker-compose.yml file is used to run multiple services an easy way. We can create and start one or more containers for each dependency with a single command.

Steps to run the application

STEP-1:

First clone this repository, With the help of the repository link type the following command in your terminal,

git clone https://github.com/gopu22/Nodejs-CRUD-API-Docker.git

This command clones the repository into your local machine.

STEP-2:

Now, change the current directory to the cloned directory,

cd Nodejs-CRUD-API-Docker

STEP-3:

With the help of the below command run the postgres container,

sudo docker compose up -d node_db
# -d : we are running in detach mode
# node_db : postgres service name

This command first checks for postgres, if it's not available it pulls the postgres from the docker registry.

STEP-4:

Check the logs,

sudo docker compose logs
# This command just give the logs information

STEP-5:

Now, let's build the docker image,

sudo docker compose build

STEP-6:

Finally, let's start the service:

sudo docker compose up node_app
# We are running the service in attach mode i.e in the foreground.

If we get the output similar to the below one, Then it is a successful connection to the postgres container and the application is running properly. Database connected Screenshot from 2023-07-26 11-30-13

Now, Let's test the application with Postman

1. Make a GET request to http://localhost:3000/
Just shows a Hello World, means it's working fine. Screenshot from 2023-07-26 11-47-04

2. Make a GET request to http://localhost:3000/orders/
We should have an empty array as a response. Screenshot from 2023-07-26 11-48-36

3. Make a POST request to http://localhost:3000/orders/
This posts/adds a new entry to the dataset order created successfully. Screenshot from 2023-07-26 11-50-20

4. Make a GET request to http://localhost:3000/orders/
To check all the added entries to the dataset. Screenshot from 2023-07-26 11-50-58

5. Make a GET request to http://localhost:3000/orders/4
Here we are making a GET request for single entry. Screenshot from 2023-07-26 11-51-40

6. Make a PUT request to http://localhost:3000/orders/4
This is the dataset before update of id-4 values. Screenshot from 2023-07-26 11-54-14

7. Make a PUT request to http://localhost:3000/orders/4
This is the dataset after update of id-4 values order updated. Screenshot from 2023-07-26 11-54-32

8. Make a GET request to http://localhost:3000/orders/
This is the updated details of the dataset. Screenshot from 2023-07-26 11-55-04

9. Make a DELETE request to http://localhost:3000/orders/5
Making a delete request for order 5 order deleted. Screenshot from 2023-07-26 11-55-50

10. Make a GET request to http://localhost:3000/orders/
After all the CRUD operations, This is the final dataset with all updated details. Screenshot from 2023-07-26 11-56-15


Data Output on pgAdmin4

Screenshot from 2023-07-26 11-57-37


Connect to the pgAdmin4 server with the help of the login credentials given in the docker-compose.yml file.
This is a user interface to view the orders dataset tables.

Screenshot from 2023-07-26 11-58-05


----------------------------------------------------THANK YOU--------------------------------------------------------

About

Building a Dockerized API application in NodeJS for Managing Orders Data in PostgreSQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published