Build Simple Nodejs API for Microservice
In this article we are gonna build Nodejs project for API and next we will integrate services with a gateway, so that is microservice.
This project we will build user service that focus for user management such as login, register and detail user. Steps to build user service are:
- Create Express project
- Install Sequelize
- Create migration
- Create seeder with fake data
- Modify Config
- Modify model
- Create API
- Register to routes
- Run in Postman
Create Express Project
For create a project, you need install Nodejs and Express in your PC. Then, you can use this command
Install Sequelize
Sequelize is a library that make our Nodejs project can using database. It features solid transaction support, relations, eager and lazy loading, read replication and more. You can read more the documentation here. For using Sequelize, you need to install sequelize and sequelize-cli (optional). But i recommend you to install sequelize-cli.
Create Migration
To create migration file, you can use Sequelize CLI. Now we create a migration file for users table with name create_table_users. You can follow this command
Inside create_table_users file, we define the fields of users table.
Create Seeder with Fake Data
Seeder is way to let us insert the initial data to our database, for generate dummy data its called Faker Factory. Next we create a seeder file with name user-seeders.
Inside the file, we will insert dummy data for users table using faker. Because we have password field that need to be encrypted, so we also need bcryptjs for encryption. Here is the user-seeders file.
Modify Config
To connect our Nodejs with database, we need some configuration in config file. How i config the configuration file are:
- Install dotenv
- Create an .env file, fill the file with PORT, DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD variables.
- import dotenv in app.js
- rename config.json to config.js
- convert json code into js code
Next, you can seed the data by npx sequelize db:seed:all
Modify Models
As default, User Model created already when we install Sequelize into our Nodejs project, so we only need to adjust the field as we declare in migration file.
Create API
For me, foldering is an important thing. With good folder management its help us to easy to maintain and scale up as well. Here is how i set the folders.
Inside handler folder we create folder as their focus. In example for user folder, its mean that folder will manage everything about user. Next we will write files inside user folder.
Login.js
This file is for create a login API only, we will check the email and password from request body with data in database. Will return success or failed depends the credential.
Index.js
This file is like a parent file to import all files inside user folder, so when we call the file it will be easier.
Now we will create a route file for users. The file is users.js inside routes.
Register to Routes
After we create file routes for users, now we need to register to app.js. Just add a line to add the users routes.
Run in Postman
To make sure our API run as expected, we can try in Postman.
From the steps above we already create APIs with Express Nodejs. You can clone my project from my Github as well. Next, we continue to build Microservice Gateway with Express Nodejs. See you!