- What is nodejsapi?
- Initial setup
- Initiate database
- Implement API server
- Pino - Logging library
- Controller structure
- How to test this API
- Containerization with docker
- References
An API to handle patient database with NodeJS back-end connected to a MySQL database server.
- Track the API performance by using NodeJS logging library Pino.
- Initiate database and handle by query commands to controll patientdb.
- Fetch and retrieve patient data by using MVC controller.
- Test API request and reponse by using httpie.
- Build docker container by using docker-compose.yml setup.
You can start nodejsapi
by following steps below.
- Initiate nodejs project:
npm init
- Install dependencies:
npm i express mysql cors dotenv ip pino-pretty
- Install nodemon as development mode:
npm i -D nodemon
- Scripts for dev mode and prod mode
"start:dev": "NODE_ENV=dev nodemon src/index.js",
"start:prod": "NODE_ENV=prod node src/index.js"
- Type setting for using javascript import, export method.
"type": "module"
- package.json
- Response test by using Httpie:
http :3000
- patients entity (init.sql)
There are different configurations in Nodejs, production and development environments. Node.js assumes that it's always running in a development environment. You can signal Node.js that you are running in production by setting the NODE_ENV=production environment
variable.
Setting the environment to production generally ensures that
- logging is kept to a minimum, essential level
- more caching levels take place to optimize performance
There is a article that compares CPU usage between development mode and production mode. As you can see, the development mode needs way more resources from CPU. It makes sense because we usually like real-time monitoring tools such as nodemon when developing applications. Therefore it is important to distinguish the Nodejs environment depending on the stage of development.
- run server by dev mode :
npm run start:dev
Logging library can give unprecedented insights into how the application is working. Having proper logging is equivalent to having a powerful telescope in a space with all applications. Pino is a Node.js logging library that attributes asynchronous logging to its fast performance. With Pino, you can send the logs to many services using proper transports like MySQL, Datadog, AWS cloud watch, or log flare. In this project, Pino provides logging messages to determine what API is doing and if there are any errors. commit
When you make a request (means request a data) to MVC application, a controller is responsible for returning the response to that request. The controller can perform one or more actions. The controller action can return different types of action results to a particular request. In this application, there are 5 actions at patient.controller.js
.
After you pull this application, you can execute API with npm run start:dev
(development mode) command inside of working directory nodejsapi
. If there is no error and conflicts, you can see the message like this.
If you want to test by using httpie, http :3000
or http :3000/patients
command will help for checking there are any issues with PORT 3000.
Docker is an open-source containerization platform. It enables developers to package applications into containers. We can handle the initial setting of docker container with docker-compose. In this application, we will build a docker image for using MySQL without install in the host computer directly. Before starting the docker-compose file, you need to check if any docker image is currently running with docker ps -a
and docker images
. We can also check the docker-compose file using the command docker-compose config
.
docker-compose up -d
command start downloading MySQL 8 version docker image and run. You can see detailed information with docker ps.
- If you already installed and are running MySQL on the host, there would be an error regard to PORT.(address already in use)
- Get into the MySQL cli mode :
mysql -h localhost -P 3306 --protocol=tcp -uroot -pletmein
You can find patientsdb
after using SHOW DATABASES
command.
And we can check patients entity information by using DESC patients
- https://www.youtube.com/playlist?list=PLopcHtZ0hJF1XfuyxnFmGmpmHrdyKO6Bx
- https://geshan.com.np/blog/2021/01/nodejs-logging-library/
- https://nodejs.dev/learn/nodejs-the-difference-between-development-and-production
- https://www.dynatrace.com/news/blog/the-drastic-effects-of-omitting-node-env-in-your-express-js-applications/
- https://www.tutorialspoint.com/mvc_framework/mvc_framework_controllers.htm
- https://www.ibm.com/in-en/cloud/learn/docker