node-js-express-sequelize-mysql-example-feature-image

Node.js Rest APIs example with Express, Sequelize & MySQL

Express is one of the most popular web frameworks for Node.js that supports routing, middleware, view system… Sequelize is a promise-based Node.js ORM that supports the dialects for Postgres, MySQL, SQL Server… In this tutorial, I will show you step by step to build Node.js Restful CRUD API using Express, Sequelize with MySQL database.

You should install MySQL in your machine first. The installation instructions can be found at Official MySQL installation manual.

Related Posts:
Build Node.js Rest APIs with Express & MySQL (without Sequelize)
Node.js: Upload/Import Excel file data into MySQL Database
Node.js: Upload CSV file data into MySQL Database

Fullstack:
Vue.js + Node.js + Express + MySQL example
Vue.js + Node.js + Express + MongoDB example
Angular + Node.js Express + MySQL example
React + Node.js + Express + MySQL example

Security: Node.js – JWT Authentication & Authorization example
Deployment: Deploying/Hosting Node.js app on Heroku with MySQL database

Node.js Rest CRUD API overview

We will build Rest Apis that can create, retrieve, update, delete and find Tutorials by title.

First, we start with an Express web server. Next, we add configuration for MySQL database, create Tutorial model with Sequelize, write the controller. Then we define routes for handling all CRUD operations (including custom finder).

The following table shows overview of the Rest APIs that will be exported:

MethodsUrlsActions
GETapi/tutorialsget all Tutorials
GETapi/tutorials/:idget Tutorial by id
POSTapi/tutorialsadd new Tutorial
PUTapi/tutorials/:idupdate Tutorial by id
DELETEapi/tutorials/:idremove Tutorial by id
DELETEapi/tutorialsremove all Tutorials
GETapi/tutorials/publishedfind all published Tutorials
GETapi/tutorials?title=[kw]find all Tutorials which title contains 'kw'

Finally, we’re gonna test the Rest Apis using Postman.

This is our project structure:

node-js-express-sequelize-mysql-example-project-structure

Demo Video

This is our Node.js Express Sequelize application demo running with MySQL database and test Rest Apis with Postman.

Create Node.js App

First, we create a folder:

$ mkdir nodejs-express-sequelize-mysql
$ cd nodejs-express-sequelize-mysql

Next, we initialize the Node.js App with a package.json file:

npm init

name: (nodejs-express-sequelize-mysql) 
version: (1.0.0) 
description: Node.js Rest Apis with Express, Sequelize & MySQL.
entry point: (index.js) server.js
test command: 
git repository: 
keywords: nodejs, express, sequelize, mysql, rest, api
author: bezkoder
license: (ISC)

Is this ok? (yes) yes

We need to install necessary modules: express, sequelize, mysql2 and body-parser.
Run the command:

npm install express sequelize mysql2 body-parser cors --save

The package.json file should look like this:

{
  "name": "nodejs-express-sequelize-mysql",
  "version": "1.0.0",
  "description": "Node.js Rest Apis with Express, Sequelize & MySQL",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "nodejs",
    "express",
    "rest",
    "api",
    "sequelize",
    "mysql"
  ],
  "author": "bezkoder",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mysql2": "^2.0.2",
    "sequelize": "^5.21.2"
  }
}

Setup Express web server

In the root folder, let’s create a new server.js file:

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");

const app = express();

var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(bodyParser.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// simple route
app.get("/", (req, res) => {
  res.json({ message: "Welcome to bezkoder application." });
});

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

What we do are:
– import express, body-parser and cors modules:

  • Express is for building the Rest apis
  • body-parser helps to parse the request and create the req.body object
  • cors provides Express middleware to enable CORS with various options.

– create an Express app, then add body-parser and cors middlewares using app.use() method. Notice that we set origin: http://localhost:8081.
– define a GET route which is simple for test.
– listen on port 8080 for incoming requests.

Now let’s run the app with command: node server.js.
Open your browser with url http://localhost:8080/, you will see:

node-js-express-sequelize-mysql-example-setup-server

Yeah, the first step is done. We’re gonna work with Sequelize in the next section.

Configure MySQL database & Sequelize

In the app folder, we create a separate config folder for configuration with db.config.js file like this:

module.exports = {
  HOST: "localhost",
  USER: "root",
  PASSWORD: "123456",
  DB: "testdb",
  dialect: "mysql",
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
};

First five parameters are for MySQL connection.
pool is optional, it will be used for Sequelize connection pool configuration:

  • max: maximum number of connection in pool
  • min: minimum number of connection in pool
  • idle: maximum time, in milliseconds, that a connection can be idle before being released
  • acquire: maximum time, in milliseconds, that pool will try to get connection before throwing error

For more details, please visit API Reference for the Sequelize constructor.

Initialize Sequelize

We’re gonna initialize Sequelize in app/models folder that will contain model in the next step.

Now create app/models/index.js with the following code:

const dbConfig = require("../config/db.config.js");

const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
  host: dbConfig.HOST,
  dialect: dbConfig.dialect,
  operatorsAliases: false,

  pool: {
    max: dbConfig.pool.max,
    min: dbConfig.pool.min,
    acquire: dbConfig.pool.acquire,
    idle: dbConfig.pool.idle
  }
});

const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.tutorials = require("./tutorial.model.js")(sequelize, Sequelize);

module.exports = db;

Don’t forget to call sync() method in server.js:

...
const app = express();
app.use(...);

const db = require("./app/models");
db.sequelize.sync();

...

In development, you may need to drop existing tables and re-sync database. Just use force: true as following code:


db.sequelize.sync({ force: true }).then(() => {
  console.log("Drop and re-sync db.");
});

Define the Sequelize Model

In models folder, create tutorial.model.js file like this:

module.exports = (sequelize, Sequelize) => {
  const Tutorial = sequelize.define("tutorial", {
    title: {
      type: Sequelize.STRING
    },
    description: {
      type: Sequelize.STRING
    },
    published: {
      type: Sequelize.BOOLEAN
    }
  });

  return Tutorial;
};

This Sequelize Model represents tutorials table in MySQL database. These columns will be generated automatically: id, title, description, published, createdAt, updatedAt.

After initializing Sequelize, we don’t need to write CRUD functions, Sequelize supports all of them:

  • create a new Tutorial: create(object)
  • find a Tutorial by id: findByPk(id)
  • get all Tutorials: findAll()
  • update a Tutorial by id: update(data, where: { id: id })
  • remove a Tutorial: destroy(where: { id: id })
  • remove all Tutorials: destroy(where: {})
  • find all Tutorials by title: findAll({ where: { title: ... } })

These functions will be used in our Controller.

We can improve the example by adding Comments for each Tutorial. It is the One-to-Many Relationship and I write a tutorial for this at:
Sequelize Associations: One-to-Many example – Node.js, MySQL

Or you can add Tags for each Tutorial and add Tutorials to Tag (Many-to-Many Relationship):
Sequelize Many-to-Many Association example with Node.js & MySQL

Create the Controller

Inside app/controllers folder, let’s create tutorial.controller.js with these CRUD functions:

  • create
  • findAll
  • findOne
  • update
  • delete
  • deleteAll
  • findAllPublished
const db = require("../models");
const Tutorial = db.tutorials;
const Op = db.Sequelize.Op;

// Create and Save a new Tutorial
exports.create = (req, res) => {
  
};

// Retrieve all Tutorials from the database.
exports.findAll = (req, res) => {
  
};

// Find a single Tutorial with an id
exports.findOne = (req, res) => {
  
};

// Update a Tutorial by the id in the request
exports.update = (req, res) => {
  
};

// Delete a Tutorial with the specified id in the request
exports.delete = (req, res) => {
  
};

// Delete all Tutorials from the database.
exports.deleteAll = (req, res) => {
  
};

// Find all published Tutorials
exports.findAllPublished = (req, res) => {
  
};

Let’s implement these functions.

Create a new object

Create and Save a new Tutorial:

exports.create = (req, res) => {
  // Validate request
  if (!req.body.title) {
    res.status(400).send({
      message: "Content can not be empty!"
    });
    return;
  }

  // Create a Tutorial
  const tutorial = {
    title: req.body.title,
    description: req.body.description,
    published: req.body.published ? req.body.published : false
  };

  // Save Tutorial in the database
  Tutorial.create(tutorial)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Tutorial."
      });
    });
};

Retrieve objects (with condition)

Retrieve all Tutorials/ find by title from the database:

exports.findAll = (req, res) => {
  const title = req.query.title;
  var condition = title ? { title: { [Op.like]: `%${title}%` } } : null;

  Tutorial.findAll({ where: condition })
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tutorials."
      });
    });
};

We use req.query.title to get query string from the Request and consider it as condition for findAll() method.

Retrieve a single object

Find a single Tutorial with an id:

exports.findOne = (req, res) => {
  const id = req.params.id;

  Tutorial.findByPk(id)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message: "Error retrieving Tutorial with id=" + id
      });
    });
};

Update an object

Update a Tutorial identified by the id in the request:

exports.update = (req, res) => {
  const id = req.params.id;

  Tutorial.update(req.body, {
    where: { id: id }
  })
    .then(num => {
      if (num == 1) {
        res.send({
          message: "Tutorial was updated successfully."
        });
      } else {
        res.send({
          message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found or req.body is empty!`
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Error updating Tutorial with id=" + id
      });
    });
};

Delete an object

Delete a Tutorial with the specified id:

exports.delete = (req, res) => {
  const id = req.params.id;

  Tutorial.destroy({
    where: { id: id }
  })
    .then(num => {
      if (num == 1) {
        res.send({
          message: "Tutorial was deleted successfully!"
        });
      } else {
        res.send({
          message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Could not delete Tutorial with id=" + id
      });
    });
};

Delete all objects

Delete all Tutorials from the database:

exports.deleteAll = (req, res) => {
  Tutorial.destroy({
    where: {},
    truncate: false
  })
    .then(nums => {
      res.send({ message: `${nums} Tutorials were deleted successfully!` });
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while removing all tutorials."
      });
    });
};

Find all objects by condition

Find all Tutorials with published = true:

exports.findAllPublished = (req, res) => {
  Tutorial.findAll({ where: { published: true } })
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tutorials."
      });
    });
};

This controller can be modified a little to return pagination response:

{
    "totalItems": 8,
    "tutorials": [...],
    "totalPages": 3,
    "currentPage": 1
}

You can find more details at:
Server side Pagination in Node.js with Sequelize and MySQL

Define Routes

When a client sends request for an endpoint using HTTP request (GET, POST, PUT, DELETE), we need to determine how the server will reponse by setting up the routes.

These are our routes:

  • /api/tutorials: GET, POST, DELETE
  • /api/tutorials/:id: GET, PUT, DELETE
  • /api/tutorials/published: GET

Create a turorial.routes.js inside app/routes folder with content like this:

module.exports = app => {
  const tutorials = require("../controllers/tutorial.controller.js");

  var router = require("express").Router();

  // Create a new Tutorial
  router.post("/", tutorials.create);

  // Retrieve all Tutorials
  router.get("/", tutorials.findAll);

  // Retrieve all published Tutorials
  router.get("/published", tutorials.findAllPublished);

  // Retrieve a single Tutorial with id
  router.get("/:id", tutorials.findOne);

  // Update a Tutorial with id
  router.put("/:id", tutorials.update);

  // Delete a Tutorial with id
  router.delete("/:id", tutorials.delete);

  // Delete all Tutorials
  router.delete("/", tutorials.deleteAll);

  app.use('/api/tutorials', router);
};

You can see that we use a controller from /controllers/tutorial.controller.js.

We also need to include routes in server.js (right before app.listen()):

...

require("./app/routes/turorial.routes")(app);

// set port, listen for requests
const PORT = ...;
app.listen(...);

Test the APIs

Run our Node.js application with command: node server.js.
The console shows:

Server is running on port 8080.
Executing (default): DROP TABLE IF EXISTS `tutorials`;
Executing (default): CREATE TABLE IF NOT EXISTS `tutorials` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255), `description` VARCHAR(255), `published` TINYINT(1), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `tutorials`
Drop and re-sync db.

Using Postman, we’re gonna test all the Apis above.

  1. Create a new Tutorial using POST /tutorials Api

  2. node-js-express-sequelize-mysql-example-demo-create-object

    After creating some new Tutorials, you can check MySQL table:

    mysql> select * from tutorials;
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    | id | title             | description       | published | createdAt           | updatedAt           |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    |  1 | JS: Node Tut #1   | Tut#1 Description |         0 | 2019-12-13 01:13:57 | 2019-12-13 01:13:57 |
    |  2 | JS: Node Tut #2   | Tut#2 Description |         0 | 2019-12-13 01:16:08 | 2019-12-13 01:16:08 |
    |  3 | JS: Vue Tut #3    | Tut#3 Description |         0 | 2019-12-13 01:16:24 | 2019-12-13 01:16:24 |
    |  4 | Vue Tut #4        | Tut#4 Description |         0 | 2019-12-13 01:16:48 | 2019-12-13 01:16:48 |
    |  5 | Node & Vue Tut #5 | Tut#5 Description |         0 | 2019-12-13 01:16:58 | 2019-12-13 01:16:58 |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    

  3. Retrieve all Tutorials using GET /tutorials Api

  4. node-js-express-sequelize-mysql-example-demo-retrieve-objects

  5. Retrieve a single Tutorial by id using GET /tutorials/:id Api

  6. node-js-express-sequelize-mysql-example-demo-get-single-object

  7. Update a Tutorial using PUT /tutorials/:id Api

  8. node-js-express-sequelize-mysql-example-demo-update-object

    Check tutorials table after some rows were updated:

    mysql> select * from tutorials;
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    | id | title             | description       | published | createdAt           | updatedAt           |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    |  1 | JS: Node Tut #1   | Tut#1 Description |         0 | 2019-12-13 01:13:57 | 2019-12-13 01:13:57 |
    |  2 | JS: Node Tut #2   | Tut#2 Description |         0 | 2019-12-13 01:16:08 | 2019-12-13 01:16:08 |
    |  3 | JS: Vue Tut #3    | Tut#3 Description |         1 | 2019-12-13 01:16:24 | 2019-12-13 01:22:51 |
    |  4 | Vue Tut #4        | Tut#4 Description |         1 | 2019-12-13 01:16:48 | 2019-12-13 01:25:28 |
    |  5 | Node & Vue Tut #5 | Tut#5 Description |         1 | 2019-12-13 01:16:58 | 2019-12-13 01:25:30 |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    

  9. Find all Tutorials which title contains ‘node’: GET /tutorials?title=node

  10. node-js-express-sequelize-mysql-example-demo-find-objects

  11. Find all published Tutorials using GET /tutorials/published Api

  12. node-js-express-sequelize-mysql-example-demo-find-active-objects

  13. Delete a Tutorial using DELETE /tutorials/:id Api

  14. node-js-express-sequelize-mysql-example-demo-delete-object

    Tutorial with id=2 was removed from tutorials table:

    mysql> select * from tutorials;
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    | id | title             | description       | published | createdAt           | updatedAt           |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    |  1 | JS: Node Tut #1   | Tut#1 Description |         0 | 2019-12-13 01:13:57 | 2019-12-13 01:13:57 |
    |  3 | JS: Vue Tut #3    | Tut#3 Description |         1 | 2019-12-13 01:16:24 | 2019-12-13 01:22:51 |
    |  4 | Vue Tut #4        | Tut#4 Description |         1 | 2019-12-13 01:16:48 | 2019-12-13 01:25:28 |
    |  5 | Node & Vue Tut #5 | Tut#5 Description |         1 | 2019-12-13 01:16:58 | 2019-12-13 01:25:30 |
    +----+-------------------+-------------------+-----------+---------------------+---------------------+
    

  15. Delete all Tutorials using DELETE /tutorials Api

  16. node-js-express-sequelize-mysql-example-demo-delete-all-objects

    Now there are no rows in tutorials table:

    mysql> SELECT * FROM tutorials;
    Empty set (0.00 sec)
    

Conclusion

Today, we’ve learned how to create Node.js Rest Apis with an Express web server. We also know way to add configuration for MySQL database & Sequelize, create a Sequelize Model, write a controller and define routes for handling all CRUD operations.

You can find more interesting thing in the next tutorial:
Server side Pagination in Node.js with Sequelize and MySQL
Return pagination data in response:

{
    "totalItems": 8,
    "tutorials": [...],
    "totalPages": 3,
    "currentPage": 1
}

Deploying/Hosting Node.js app on Heroku with MySQL database

Or you can save Image to MySQL database:
Upload/store images in MySQL using Node.js, Express & Multer

Happy learning! See you again.

Further Reading

Upload Tutorial data from file to MySQL database table:

Source code

You can find the complete source code for this example on Github.

If you want to add Comments for each Tutorial. It is the One-to-Many Association, there is a tutorial for that Relationship: Sequelize Associations: One-to-Many example – Node.js, MySQL

Or you can add Tags for each Tutorial and add Tutorials to Tag (Many-to-Many Relationship):
Sequelize Many-to-Many Association example with Node.js & MySQL

91 thoughts to “Node.js Rest APIs example with Express, Sequelize & MySQL”

  1. Any chance of combining this with your express vue jwt authentication tutorial so only admins can edit or delete tutorials?

  2. This tutorial shows me a basic & best way about MVC structure using Node.js Express and Sequelize. Thanks for sharing..

  3. Thank a lot, would you like to use koa2 instead of express in next tutorial. Your tutorial is easy to understand for beginner!!

  4. Got up to using postman, my backend seems to be working and is creating the table on mysql server but I cant seem to get a post request to work. I cant get any response. Have never used the app could you go into a bit more detail on getting it to work.

    1. Hi, you should set appropriate Header: Content-Type: application/json when sending POST request (Postman Header tab).

      1. Yup, I am so lost on how to connect this to the vue.js front end. I manage to get this working on a raspberry pi, but postman is not a library that’s supported on the OS.

  5. Hey man! I’m taking an effort to commend you for this amazing tutorial. I’m an iOS Engineer and I’m very much new to Node express. I first saw your Customer Node Express + Controller tutorial, but I need a Sequelizer, and then I found this. πŸ™‚

    I hope you could continue this as a series and provide authentication stuff.

    Much love from PH.

    G

  6. Hello Bezkoder,
    thank you very much for your tutorial, however I have a problem when posting to create tutorial,
    the parameters “title” and “description are always “NULL”, others one are okay. I inserted directly in mysql too, it’s working. Any infos? Thanks in advance.

  7. Hello, first of all thanks for the awesome tutorial.
    However I am not being able to get the PUT(update using id) and Delete (by id) to work in Postman.
    I am getting the could not get any response message. However the get by id,get all ,Post and delete all work fine.

  8. THIS WAS REALLLLLLLLLLY AWESOME !!! I WAS CONFUSED HOW TO CONNECT VUE.JS WITH NODE.JS AND YOUR POST IS SIMPLE AND EFFICIENT…

    thanks πŸ™‚

  9. Hi! I stumbled across an issue, i want to work with tables that have foreign keys, but I haven’t been able to incorporate sequelize documentation about fk’s or migrations. Any tips on doing so?

  10. Thank you so much for this tutorial.
    I’m waiting for new React tutorial, example about associate authentication jwt reactjs nodejs CRUD.

  11. Great tutorial! Very easy to follow for a rookie like me πŸ™‚
    I have one question – I can’t seem to find where you point to your “tutorials” table in your MySQL connection? If I want it point to another table (like the table “clients”) where would I do that?

    Rue

    1. Sorry, all work! bezcoder…. thank you for you example lesson. Ну ΠΈ ΠΏΠΎ русски – спасибо.

  12. The tutorial is very good and helpful. I tried to use the client Web app to call the mySQL api sample. It shows below error. ( I use MySQL 8.0.20, Angular 8).
    :8080/api/tutorials:1 Failed to load resource: net::ERR_FAILED
    add:1 Access to XMLHttpRequest at ‘http://localhost:8080/api/tutorials’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The ‘Access-Control-Allow-Origin’ header has a value ‘http://localhost:8081’ that is not equal to the supplied origin.
    add-tutorial.component.ts:35 HttpErrorResponse
    Thank you very much!

    :8080/api/tutorials:1 Failed to load resource: net::ERR_FAILED

  13. Hi! Thank you so much for this!! Every time I try to run the POST request (the first step of the Testing APIs section), I get “message”: “Content can not be empty!”. I have ‘Content-Type’ ‘application/json’ set in my header tab. Do you have any tips? Thank you so much!

  14. Very great tutorial! but i have a problem, my api keep loading and never finished, anyone know how to solve this?
    nothing error from executing “nodemon app.js”

  15. I’ve been struggling with this for hours, and this tutorial finally helped me get it! I can’t thank you enough!!

  16. PLEASE REPLY. THANK YOU.
    Hello, ive followed the instructions step by step but keep getting this error and the api isnt responding on postman:

    (node:10716) [SEQUELIZE0004] DeprecationWarning: A boolean value was passed to options.operatorsAliases. This is a no-op with v5 and should be removed.
    Server is running on port 8080.
    (node:10716) UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user ‘root’@’localhost’ (using password: YES)
    at ConnectionManager.connect (C:\Users\Ecstasy-Net\nodejs-express-sequelize-mysql\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:118:17) at processTicksAndRejections (internal/process/task_queues.js:97:5)
    (node:10716) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `–unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:10716) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

    1. Hi, you should create database with correct host, username, password, port. Then modify the configuration in the source code.

    2. in db.config.js file delete the line where it says ( “operatorsAliases”: false ), it’s version 5 so it’s depreciated, it out date in a way.

      1. Hello Sam,
        I’ve been struggling on the same issue because it was my very first time with mysql.
        First you need to install mysql-server on your computer.

        //installation
        shell> sudo apt install mysql-server
        //Start the service
        shell> sudo systemctl start mysql-server
        //Then to enter your mysql instance
        shell> sudo mysql
        //create a user
        mysql > CREATE USER ‘newuser’@’localhost’ IDENTIFIED BY ‘password’;
        //give to the newuser all PRIVILEGES
        mysql > GRANT ALL PRIVILEGES ON * . * TO ‘newuser’@’localhost’;
        //then flush
        mysql > FLUSH PRIVILEGES;
        //create the Database
        mysql > CREATE DATABASE ‘dbName’;
        //quit
        mysql > quit

        Now that you’ve created a user you can log in your mysql-server through a shell thanks to
        shell> mysql -u USERNAME -p
        or directly to the database with
        shell> mysql -h host -u user -p dbName

        Don’t forget to replace the variable in your db.config.js project’s file.
        Please for more information check https://dev.mysql.com/doc/
        Paulo

  17. It’s time to take care of a key point on our RESTful API: the security. We have our endpoints to manage users and tasks, but right now any client that knows where our API is running can access the endpoints. This is not safe, in the real world you’d want to control who has access to your API and you need to check with every request that it’s coming from a trusted source.

  18. Hii, first of all its a really helpful tutorial and easy to understand to a newbie like me.
    Just one query I have for you.
    Here you just have one table in the database but I have 9 tables in my project which are linked with each other via foreign keys.
    To get, update or delete information I have to join 2 or more tables and update all the fields . How can I do it using this code as a reference .
    Also if I update data in the front end will it reflect in my MySQL database. Please clear this doubt as well.
    Please help me with this.
    Thanks in advance πŸ™‚

  19. Thanks for your tutorial,
    I just have a question: In the server.js file, how to use to routes instead of one

    require(“./app/routes/turorial.routes”)(app);

  20. Please Reply,
    I tried to coding follow you but it show this erorr.

    (node:25800) UnhandledPromiseRejectionWarning: SequelizeConnectionRefusedError: connect ECONNREFUSED 139.59.223.169:3306
    at ConnectionManager.connect (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:116:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ConnectionManager._connect (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:318:24)
    at async /Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:250:32
    at async ConnectionManager.getConnection (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:280:7)
    at async /Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/sequelize.js:613:26
    at async MySQLQueryInterface.dropTable (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/dialects/abstract/query-interface.js:243:5)
    at async Function.drop (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/model.js:1388:12)
    at async Sequelize.drop (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/sequelize.js:849:33)
    at async Sequelize.sync (/Users/sparghetti/Projects/surveys-api/node_modules/sequelize/lib/sequelize.js:775:7)
    (Use `node –trace-warnings …` to show where the warning was created)
    (node:25800) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `–unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:25800) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

  21. Not able to find request body for http post method :
    Cannot read property ‘title’ of undefined
    at exports.create (F:\projects\sample1\controller\tutorial.controller.js:11:18)
    at Layer.handle [as handle_request] (F:\projects\sample1\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\projects\sample1\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\projects\sample1\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (F:\projects\sample1\node_modules\express\lib\router\layer.js:95:5)
    at F:\projects\sample1\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (F:\projects\sample1\node_modules\express\lib\router\index.js:335:12)
    at next (F:\projects\sample1\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (F:\projects\sample1\node_modules\express\lib\router\index.js:174:3)
    at router (F:\projects\sample1\node_modules\express\lib\router\index.js:47:12)

  22. Hello,
    I tried to combine two of your tutorials together Node.js Rest APIs example with Express, Sequelize & MySQL and Server side Pagination in Node.js with Sequelize & MySQL. The problem appears when I press edit and the next page doesn’t appear even thought the id of specific tutorial in URL changes. When i did your tutorials separably everything worked fine. That’s how it looks like https://i.imgur.com/zfM0v9f.png

    1. I’m sorry, I made a mistake with one of tutorial’s name I meant React + Node.js Express: User Authentication with JWT example and Server side Pagination in Node.js with Sequelize & MySQL

  23. One cannot find an easier well explained tutorial for connecting MySql with Node JS
    Great Work Man !!!πŸ‘ŒπŸ‘ŒπŸ‘ŒπŸ‘ŒπŸ‘Œ

  24. I just worked my way through it and it worked fine. Excellent tutorial. Thank you, Now to keep going and find out more about the Front end
    πŸ™‚
    Cheers.

  25. Hello Sir, I’m really thankful for this tutorial because I just recently learning Node.js, React.js, and MySQL individually and don’t have the idea how to connect them all. So, thank you to you, Sir!

    I just started with this tutorial to make node.js backend and follow all the steps above but I have errors like this:

    Server is running on port 8080.
    (node:16820) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Unknown database ‘testdb’
    at ConnectionManager.connect (C:\Users\user\nodejs-express-sequelize-mysql\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:126:17)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    (node:16820) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `–unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:16820) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
    the Node.js process with a non-zero exit code.

    It seems that there is no testdb database, so, am I missed out some tutorial before this? or any suggestion from you Sir to resolve this error?

    Please Kindly Reply πŸ™‚

      1. okay, so just an empty database then the title, description, and published will be created in localhost:8080/api/tutorials ? thank you sir!

  26. Helloo.. thanks for this post.. its really help me a lot
    i’m new in website developing.. i wanna ask, how to upload the vue js + this node.js code into web server?
    thank you so much bezkoder!

  27. Hi, Bezkoder! This is an excellent tutorial!
    There’s just a line of code that I really do not understand.
    This line: require(“./tutorial.model.js”)(sequelize, Sequelize)
    I don’t get it. What’s supose to do (sequelize, Sequelize), is this something related with JavaScript, with the node’s require function or is it because of sequelize. I’ve been checking the sequelize documentation but I didn’t see anything similar. πŸ™

    I’d appreciate a lot if someone could explain me.

    1. Sorry!!! Forget it. I hadn’t seen how the tutorials model is defined.

      module.exports = (sequelize, Sequelize) => {
        const Tutorial = sequelize.define("tutorial", {
          title: {
            type: Sequelize.STRING
          },
          description: {
            type: Sequelize.STRING
          },
          published: {
            type: Sequelize.BOOLEAN
          }
        });
      
        return Tutorial;
      };
      

      Now I understand is a JavaScript thing. lol

Leave a Reply

Your email address will not be published. Required fields are marked *