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.
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.
Now, change the current directory to the cloned directory,
cd Nodejs-CRUD-API-Docker
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.
Check the logs,
sudo docker compose logs
# This command just give the logs information
Now, let's build the docker image,
sudo docker compose build
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
1. Make a GET request to http://localhost:3000/
Just shows a Hello World
, means it's working fine.
2. Make a GET request to http://localhost:3000/orders/
We should have an empty array as a response.
3. Make a POST request to http://localhost:3000/orders/
This posts/adds a new entry to the dataset order created successfully
.
4. Make a GET request to http://localhost:3000/orders/
To check all the added entries to the dataset.
5. Make a GET request to http://localhost:3000/orders/4
Here we are making a GET request for single entry.
6. Make a PUT request to http://localhost:3000/orders/4
This is the dataset before update of id-4 values.
7. Make a PUT request to http://localhost:3000/orders/4
This is the dataset after update of id-4 values order updated
.
8. Make a GET request to http://localhost:3000/orders/
This is the updated details of the dataset.
9. Make a DELETE request to http://localhost:3000/orders/5
Making a delete request for order 5 order deleted
.
10. Make a GET request to http://localhost:3000/orders/
After all the CRUD operations, This is the final dataset with all updated details.
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.