Creating Complete REST API using Node js Express and MySQL

Posted at: March 26, 2019 8:12 PM

How to create RESTful API in Node.js, Express and MySQL?

A RESTful (Representational State Transfer) API is an application program interface (API) that uses HTTP requests to GET, POST, PUT and DELETE data. If you want to interact with a service, you generally use an API. Node.js is best the open source server environment for creating an API.

In this lesson we will learn how to create rest api using Node.js, Express and MySQL Database with request methods GET, POST, PUT and DELETE.

I have taken an example for calling api in PHP using CURL. You can use this api in technology like as Angular, React, DotNet, PHP, etc.

Setup Environment

First of all, Have installed Node.js in your system? if Yes then follow next steps otherwise you can watch this video Node.js Installation.

Intallation express-generator tool

We will install express-generator tools globaly. If you have already installed express-generator tool then no need to install.

Create a Application

Create a application using express-generator tool

Install Dependencies

Install mysql node modules in node.js

Before use mysql in Node.js must be ensured MySQL Database should be installed in your machine. You can see Installing MySQL documentation.

Install mysql node modules in your application.

Create a Custom Module for Database Connection

Create a db.js file in the root folder for database connection and use this connection in your entire project. Connect your database using your MySQL database credential host, database user, database password and database name.

db.js

Create Route

Create a file routes/products.js for add, edit, views and delete products.

routes/products.js

Edit app.js

Edit app.js file and import var productsRouter = require('./routes/products'); and set products route app.use('/products', productsRouter);

Complete code app.js

Calling API in PHP

We are calling this api in PHP using CURL. But you can call this api in any technologies like as Angular, React, etc.

MySQL

Create a table products

Run the app

Then load http://localhost:3000/ in your browser to access the app.

You can check this url http://localhost:3000/products for products json data.

Calling API

You can run above PHP code on your machine for calling this api with all methods GET, POST, PUT and DELETE

Conclusion

In this lesson we have learned setup of Node.js environment, installation of express-generator tool and create an application using express-generator. Create a complete RESTful API in Node.js, Express and MySQL. We have used request methods GET, POST, PUT and DELETE and called this api in PHP.

node.js installation express GET POSTPUTDELETEMySQLPHP node js api node js rest api framework node js rest api client node js rest api mysql how to create rest api in node js with mysql node js crud mysql express generator rest api

Please leave comments

2 Comments

truecodex launched free android app. You can download and install truecodex app on your mobile and get notification of the latest post.

Video Tutorials on YouTube