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.
You are going to build following endpoints:
|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
express-generator globally. It means you can access from anywhere via terminal.
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
After generating code, your folder structure should be similar to below shown
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.
After installation, let use the CLI to generate migrations, seeders, config and models directories and config file.
I got following log in my console:
sequelize init command, your folder structure should be similar to below shown
If you are windows user correct the config file path in
models/index.js by changing
var config = require(__dirname + '/..\config\config.json')[env];
var config = require(__dirname + '/../config/config.json')[env];
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
migration file in the
will be the date the model was generated.
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.
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
routes directory with following code:
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,
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.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.