Create Restful CRUD API with Node js MySQL and express

Create Restful CRUD API with Node js MySQL and express

In this article, we will show how to create restful CRUD API with node js MySQL and express. here we will create a simple restful API using node js. so we can easily manage the HTTP request (such as GET, POST, PUT, DELETE, etc.).

so you can follow the below steps for create restful restful crud api with node.js express and mysql.

Overview

Step 1: Create the Application Directory.

Step 2: Create an application using npm init

Step 3: Install helpful Packages

Step 4: Create database and table using MySql

Step 5: Setting Database Configuration

Step 6: Create an app.js file

Step 7: Run Application

Building a Restful crud API with node.js

Step 1: Create an Application Directory

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

Step 2: Create an application using npm init

The “npm init” command through we can create a new package.json file in our application directory. when we build a new app at that time we use this command. Node.js will need to have a package.json file because includes the application related packages and application data.

Here below command helps to initialization our application.

Step 3: Install helpful Packages

Expess.js is a free and open-source framework and it’s a web application framework for Node.js. we can manage routes, create restful API, handle the request and response, and also create MVC structure using the express.js. Npm stands for the node package manager. npm, help to install node packages and manage the types of dependencies.
Here below command help to install express package.

This body-parser module parses JSON request and that allows you to access req.body from within your routes.

below following command using install the MySQL module

The node-datetime npm package is used for creating date and time.

Step 4: Create database and table using MySql

Create MySQL table

we can use the below command to create a database, otherwise, we can directly create a database on PHPMyAdmin.

Step 5: Setting Database Configuration

Node js database connection with MySQL

After complete the installation of packages. we will database configuration. now we will create config.js in the application directory and paste the below code in this file.

Step 6: Create a server.js file

Now, we will take a new file and paste follow below code in this file and save as server.js.

node js server setup

Rest API Post Example

NodeJS MySQL Insert into Table

Rest API Get Example

Rest API Get Example

Rest API Get Example

Rest API Put Example

Rest API Put Example

Rest API Delete Example

Rest API Delete Example
Step 7: Run Application

Now, you can run your application using the following command.

So, you can following URL

If you liked this article, you can also like and share it and download it through download url.

Please follow and like us: