Build Your First API With Express Generator, Sequelize and CLI
(A step-by-step guide for absolute beginners)
Express generator is a package that simply saves you time and effort in creating node.js web apps. It does this by generating folders that you would otherwise have created manually.
So, how do we achieve this:
Step 1: open your terminal using the short cut (Cntrl +Alt +T), then change the directory to your desktop using the command:
$ cd Desktop
Step 2: create a folder and give it any name of your choice. Use the command:
$ mkdir folderName
Change the directory to your folder using the command:
$ cd folderName
Step 3: next, lets generate the folders (public, routes, views, app, bin, and package.json folders) using express generator. Use the command:
$ npx express-generator
Your folder should have the following structure:
Your folder
├── app.js
├── bin
│ └── www
├── package.json
|── package-lock.json
|── node_modules
├── public
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── style.css
├── routes
│ ├── index.js
│ └── users.js
└── views
├── error.pug
├── index.pug
└── layout.pug
Step 4: Next install dependencies (Express generator defines the dependencies that we need, but doesn’t install them. To install dependencies, use the command:
$ npm install
Step 5: open your folder with the command
$ code .
Now lets explore the folders we have created:
bin: bin contains the port which our app has to listen to.
node_modules: this folder contains numerous packages and functionalities necessary for our app to function
public: the public folder holds all the images, third-party javascript files, and stylesheets.
routes (contains index and users files by default): the routes folder determines links that will be functional.
views: The views allows us to create HTML templates that can be applied to pages.
app.js: this is the entry point to our app. In the app.js, the dependencies are required. The routes are also required and we tell express to use the routes. it also contains other configurations to tell express how to handle errors.
package.json: this file contains the list of dependencies.
package-lock.json: this folder contains details about the application.
Once your folder opens in the editor (VScode in my case), press cntrl ~ to open the inbuilt terminal and continue working therein.
Step 6: Now, we install sequelize, which is an ORM that connects Nodejs to your database. We use the command ```npm install — save sequelize. This adds sequelize to your list of dependencies in the package.json file.
“dependencies”: {
“cookie-parser”: “~1.4.4”,
“debug”: “~2.6.9”,
“express”: “~4.16.1”,
“http-errors”: “~1.6.3”,
“jade”: “~1.11.0”,
“morgan”: “~1.9.1”,
“sequelize”: “6.3.5”
}
Step 7: next we start the sql database (XAMPP in my case) by using the command:
sudo /opt/lampp/lampp start
Step 8: we now install the mysql library by using the command:
npm i mysql2
This adds mysql2 to the list of dependencies as seen below
“dependencies”: {
“cookie-parser”: “~1.4.4”,
“debug”: “~2.6.9”,
“express”: “~4.16.1”,
“http-errors”: “~1.6.3”,
“jade”: “~1.11.0”,
“morgan”: “~1.9.1”,
“mysql2”: “2.2.5”,
“sequelize”: “6.3.5”
}
Next, go ahead to manually create your models or better still, use Sequelize CLI to save time and effort.
Step 9: first begin by installing Sequelize CLI using the command
npm install --save-dev sequelize-cli
It is saved as a devDependency because it is required only for development purpose.
“dependencies”: {
“cookie-parser”: “~1.4.4”,
“debug”: “~2.6.9”,
“express”: “~4.16.1”,
“http-errors”: “~1.6.3”,
“jade”: “~1.11.0”,
“morgan”: “~1.9.1”,
“mysql2”: “².2.5”,
“sequelize”: “⁶.3.5”
},“devDependencies”: {
“sequelize-cli”: “⁶.2.0”
}
Step 10: next we initialize sequelize CLI by using the command
npx sequelize-cli init.
This creates four folders: config
, migrations
, models (contains index by default)
, and seeders.
Step 11: next, make the following changes to the content of your config/config.json file -
Change the values of your “password” and “database” properties as follows:
“password”: “ ”,
“database”: “your_database_name”
Step 12: next, we create the database we have just named using the command:
npx sequelize-cli db:create.
Confirm database creation by checking phpMyAdmin.

Step 13: next, we setup a model file and a migration file for a table we would call “User”. We achieve this by using the command:
npx sequelize-cli model:generate — name User –attributes name:string,email:string,password:string
Step 14: next, we create the “Users” table in the sql database using the command:
npx sequelize-cli db:migrate

Next, lets create some functional routes and controllers.
Lets begin by understanding what controllers are: Controllers contain the functions of routes. This helps to reduce the content of the route.
In the steps that follow, I will show you how to create a post route (users/register) in the user route
Step 15: create a folder “controllers” and a file inside named user.controller.js
Step 16: require the user.controller file in the users.js route file by declaring a constant called userController
const userController = require(‘../controllers/user.controller’)
Step 17: create a post route for register
router.post(‘/register’, userController.register)
Step 18: export the module
module.exports = router;
Next, return to the user.controller file to set up the function for the post route you just created
Step 19: in the user.controller file, require the index.js file located in the model and assign it a constant we would call “models”
const models = require (‘../models’);
Step 20: create a constant User which equals models.User (this refers to the user.js file in the model)
const User = models.User
Step 21: next, create a function register that carries two parameters (request and response). The request is the body of input that the user inputs to register, and response is the message that displays to show that registration was successful. Then export the function. Your code should look like this:
const models = require (‘../models’);
const User = models.User
const register = async (req, res) => {
const data = req.body;await User.create(
data
);
res.json(“Registration successful”);
}
module.exports = {
register
}
Step 22: run the app by typing npm start. Then test with postman (download postman here: https://www.postman.com/downloads/)

Step 23: Check that the record has been created in your database
