Node js Express Sequelize and Mysql Example

Node js Express Sequelize and Mysql Example

In this article, we will explain to you how to create Rest APIs example with Express, Sequelize & MySQL in node js(Node js Express Sequelize and Mysql Example).

Express is the most famous web framework for Node.js that supports routing, middleware, view system. Sequelize is a promising Node.js ORM that supports offers for Postgres, MySQL, SQL Server. In this tutorial, we will show you step by step to create Node.js Restful CRUD API using Express, Sequelize with MySQL.

So you can see our node js example.

Application Directory Structure

We have created the best directory structure for the rest API CRUD operations. so you can follow the directory structure below.

Create Node Js Application

First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.

The run “npm init” command through we can create a new package.json file in our application directory

Install NPM Package

After the done setup node js application, we will install the express, sequelize, mysql2 and cors npm package. so you can see the following npm command.

Setup Express web server

In this step, We will create the server.js file in our application directory. after then setup the our application.
server.js

Setting up Sequelize MySQL connection

In this step, we will create .env file and setup database connection using the sequelize with mysql.
app/config/env.js

app/config/db.config.js

Create Sequelize model

Now, We create the sequelize model and which is define database schema to this sequelize model. so you can see user.model.js file as example.
app/model/user.model.js

Create controller

Here, we create the some different methods such as create, findall, update, and delete. which is helpful for getting data, insert data and update data with mysql.
app/controller/user.controller.js

Create Route

Now, We will define the all users route. so you can see our route example.
app/route/user.route.js

Run Node js Application

we will run the node js application using the below command. so you can following the below command.

Now you can run the example using the below Url in the browser.

Please follow and like us: