REST (Representational State Transfer) is Architectural Styles and a Design of Network-based Software Architectures. Now a days REST architecture is widely used in application development. You can use it with any application to verify or view specific information on web without invoking server side validations.



We have already discussed how to implement API using Slim micro framework and how to create RESTFul API using Node.js, Express.JS and MySQL. In this tutorial we will see how to create an API using JavaScript rest API framework NodeJS, Express and MongoDB.

Read more:

Create a RESTful Web Service API with NodeJS, ExpressJS and MySQL

Create a RESTful Web Service API with Slim framework

 

MongoDB is a open-source and cross-platform database, like NoSQL database. MongoDB is document-oriented database that uses JSON Documents with schemes.

Read more about basic commands that each Mongo-DB developer should know.

Restful API using Node JS, Express and MongoDB

RESTFul API Using Node.JS, Express.JS and MongoDB

Before starting with node js MongoDB tutorial we need a hosted MongoDB server to be connect with our application. To run MongoDB server open terminal or command line if you are having windows operating system. Navigate to MongoDB directory and run below command.

To make debug easy you can install windows MongoDB client such as MongoBooster.

Directory Structure

We are going to use jade template engine to render 404 page. Jade is high performance template engine implemented with JavaScript for node.

Installing Node Packages

Run below command to install Node packages

It will create package.json file. Now install Jade by running following command.

Install body-parser by using following command.

Now, we need to install express and MongoDB. It install express js use below command –

It will install latest version of express and update package.json as well. After completing installation of express finally we need to install MongoDB node package.

After installing all the modules our package.json will look like

You can change information such as application name and version in package file.

404.jade

This file will contain error message along with current URL.

Setup Application Server

For initial setup open the app.js and complete setup by copying below code.

We are using nodejs-shop database in application. Before setting port of server make sure port is not being used by another application or service. Default route is nothing but an index page from where you can double check either service started or not. We will start node server at the end of tutorial when we will be going to test the web server.

collectionDriver.js

collectionDriver.js file contains all the database related functions that need to be used in later part of tutorial.

Add Product

Create a route having specific URL and HTTP Method. MongoDB will create database and table on the fly so we do not need to take care it like MySQL database.

We have used post HTTP method. You can pass any parameter for product in request MongoDB will create fields automatically based on your request parameter key.

It will create a JSON document and insert information into database such as Product name, Product Price, Product Description. We will use Postman Chrome Extension to test our App. You can install the extension from here.

Response:

Get Single Product

Single Product endpoint will returns entity from “md_products” table into JSON format based on id passed in request. if record not found having id passed by user than it will return error with 400 HTTP code.

Response:

Edit Existing 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 PUT method with our request.

Response:

Delete Product

This endpoint will check wether record is exists in database having requested id or not. If record found the it will delete the record by calling mongoWrapper.delete() function. If record does not exists in database than it will return error with 400 HTTP code.

Response:

We have completed crud operations in Nodejs server. Now open your command line Run your application by node app.js command. Open “http://127.0.0.1:8080” in browser to see the output.

 

Read More:

Create RESTful API Using Node.JS, Express and MySQL

Create a RESTful Web Service API with Slim

How to Create Database in MongoDB

Integrate PayUMoney Payment gateway in PHP

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

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="">