Build a RESTful API Using Node, Express and sequelize

By reading this tutorial you can build RESTful APIs using Node, express, and sequelize ORM. You gonna use the different HTTP methods during the REST API development, quick introduction to each method.

GET to retrieve data
POST to add data
PUT to update data
DELETE to delete data

Each method as a purpose isn’t it?. Let me give an introduction to the app which you are going to build is TODO application. You can add a todo to list, update, delete todos and you can even search for todos.

You can read async and await version of this post at – RESTful API Using async, await – Node, Express and sequelize

Build a RESTful API Using Node, Express and sequelize

You are going to build following endpoints:

Method URL Action
GET /todos Retrieve all todos
GET /todos/search/bug Search for todos with ‘bug’ in their name
GET /todo/1 Retrieve todo with id == 1
POST /todo Add a new todo
PUT /todo/1 Update todo with id == 1
DELETE /todo/1 Delete todo with id == 1

Install sequelize-cli and express-generator

Following npm command installs sequelize-cli and express-generator globally. It means you can access from anywhere via terminal.

Project setup

Lets create an Express app named "todos-manager" with express generator. With following command the app will be created in a folder named todos-manager in the current working directory and the view engine will be set to Pug:

After generating code, your folder structure should be similar to below shown
Build a RESTful API Using Node, Express and sequelize

Install sequelize

Sequelize is an easy-to-use multi SQL dialect ORM for Node.js. We gonna use MySQL as our database.So let install Sequelize ORM and mysql2 dialect.

Initializes sequelize

After installation, let use the CLI to generate migrations, seeders, config and models directories and config file.

I got following log in my console:

After executing sequelize init command, your folder structure should be similar to below shown
Build a RESTful API Using Node, Express and sequelize

If you are windows user correct the config file path in models/index.js by changing
var config = require(__dirname + '/..\config\config.json')[env];
to var config = require(__dirname + '/../config/config.json')[env];

Database Configurations

Now config.json file which is located at config\config.json and update your database details,

Create Models and Migrations

Now again we gonna use sequelize cli command to generate model and migrations files.

Above command generates a todo.js file in the PROJECT_ROOT/models folder as well as a -create-todo.js migration file in the PROJECT_ROOT/migrations folder. will be the date the model was generated.

Create Models and Migrations


Here is the generated model code, you can add or remove columns to it, make sure to update migration file as for your changes on this model.


Here is the generated migration code, you can add or remove columns to it, make sure to update model file as for your changes on this migration file.

Run Migrations

You can create database tables by running migrations with:

That’s it, now you can check your database, you should able to see new tables in your database.

Implementing the API calls with Express


Let’s create a file called todos.js in routes directory with following code:

Now require todos.js file inside the app.js file just like other route files.

List of todos

Lets write a api call to get list of todos, Open your todos.js file and update get method with following code.

Test the request out in the browser – http://localhost:3000/todos. You will get an empty array of your todos table is empty, if you have data in todos table, you will get data in JSON format.

Creating a todo

Let’s make a post request to make a todo entry in the database,

You can test with any rest client tools like Advanced REST client, or Postman, but I am going to use Curl.

Updating a todo

To test issue following command,

Deleting a todo

To test issue following command,

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Sahil Saini
Sahil Saini
2 years ago

good job (y)

2 years ago
Reply to  Sahil Saini

Thank you!

Rami Kelesli
Rami Kelesli
2 years ago

can you demonstrate how we can use on google cloud

gabriel amador
gabriel amador
2 years ago

I’m using this for an api. The problem is that one of my fields is a blob, and I get it as a list of integers instead of like a text or something like that. As I’ve been seeing, it seems that sequelize sees the blobs as buffers. How could I edit the way a field is presented? Thanks Protection Status
Would love your thoughts, please comment.x