Skip to content

An API to handle patient database with NodeJS back-end connected to a MySQL database server.

Notifications You must be signed in to change notification settings

nhistory/nodejsapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

  1. What is nodejsapi?
  2. Initial setup
  3. Initiate database
  4. Implement API server
  5. Pino - Logging library
  6. Controller structure
  7. How to test this API
  8. Containerization with docker
  9. References

What is nodejsapi?

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.

Initial 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

image

  • Response test by using Httpie: http :3000

image

Initiate database

  • patients entity (init.sql)

image

Implement API server

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

image

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

Pino - Logging library

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

Controller structure

image

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.

image

How to test this API

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.

image

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.

image

image

Containerization with docker

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.

image

And we can check patients entity information by using DESC patients

image

References

About

An API to handle patient database with NodeJS back-end connected to a MySQL database server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published