Today we’ll learn to create a RESTful API using Node.js, Express 3 and MySQL. Before starting with node js (Official site) tutorial and creating an API we need to learn about what is node js. So, Nodejs is JavaScript rapid framework which uses an event-driven model that makes it lightweight and faster. Now a days, Node.js is widely used for App development.

The Express 4 is Node framework that provides robust set of features to develop mobile and web applications. It is used to perform different actions based on URL and HTTP Method.

Read More:

Create a RESTful Web Service API with Slim

We are going to use latest nodejs version v6.10.2. Make sure you have installed Node.JS and let’s get to it!

Create REST API Using Node.JS, Express and MySQL

RESTful API Using Node.JS, Express and MySQL with Example

We are going to build an API for a Shop that will:

  • Handle CRUD for an item (Eg. Products)
  • Return JSON data
  • Use the proper API Endpoints (URL) and HTTP methods (POST, GET and DELETE)
Directory Structure

 Download Tutorial

MySQL Database

To create new MySQL database open http://127.0.0.1/phpmyadmin in your browser (you can also use MySQL client) and create a database node_shop in MySQL.

It will create nd_products databse table which we will use later in this tutorial. Now, Insert some test data into database table by executing following queries.

Installing Node Packages

To install node packages open command line and navigate to application root directory and run below commands one by one.

npm will create node_modules folder and download all the packages into it.

After installing all Node packages package.json will look like below.

Setup Server

Copy below code into server.js and save it into root folder of our application.

Basic Setup: Pull the packages installed by npm and define them in our app so we can use it later.
Setup express: Setup an App with express, configure app to use body parser and assign port to our app.
Create server: Create server that will listen specified port that we defined earlier.
Database Connection: Configuration related to our database connection. You can replace your credentials with our credentials.

It’s time to start server. Run below command on the root directory of our application

We will use Postman Chrome Extension to test our App. You can install the extension from here.

Add Product

This endpoint will add product information into database. All the fields are mandatory in request.

Response:

Get Single Product

It will search a product record having id set by user from nd_products database table and if it found the record than it will send product details data back into JSON response. If product could not found in database table than will show message “No Records Found” with result as error.

We are sending response information in JSON format. This is standard for an API and will help the App developer to use our data.

Response:

Postman Output
Postman Get Single Product Response - Node.JS Tutorial

Edit Single Product

An Edit Single Product endpoint will be used to edit specific product exists in ‘nd_products’ database table. The parameters Product name, price and image URL need to be passes in POST method with our request. Server will make sure that no any parameters is not missing in request. If any of these parameter could not found than an error response with “Please fill required information” will be sent in response in JSON format.

Now, let’s send request to our application using Postman, Appropriate response will get back in response from our application.

Response:

Postman Output
Postman Edit Product Response - Node.JS Tutorial

Delete Single Product

In this route, we are searching a product in our nd_products database table having product id set in request, if product is exists in database table than delete that particular product.

If any error occurred in database operation during request, we will send response with 400 HTTP code and error description.

Response:

Postman Output
Postman Delete Product Response - Node.JS Tutorial

server.js (Full Code)

 Download Tutorial

That’s all:) Our basic CRUD operation REST API is ready to use. I will share more node js tutorials soon.

Do comment or contact me at for any kind of help during development.

 

Read More:

Create a RESTful Web Service API with Slim

How to Integrate PayUMoney Payment gateway in PHP

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

sFileTypeChecker – JQuery Plugin To Verify Input File before uploading

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

Composer – Dependency manager for PHP

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">