Posted on Updated on - Arjun

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 about 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 about 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.

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

PROJECT_ROOT/models/todo.js

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.

PROJECT_ROOT/migrations/20170921180639-create-todo.js

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 you database, you should able to see new tables in your database.

Implementing the API calls with Express

Routes

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 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.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status