Building a Restful CRUD API with Node.js Express and MongoDB

In this article we have explained about Building a Simple CRUD Restful app with Node, Express, and MongoDB. CRUD stands for Create, Read, Update and Delete. These are the basic operations that a simple web app would be designed to achieve. If you don’t know about REST before, you can read more about it here.

Image for post
Image for post

Getting Started

  1. Install Node.js from the Node.js website
  2. Create a directory called as node-restful-curd-app in your local machine.
  3. Open terminal and check your node version using this command node -version
  4. Initialize the application with a package.json file

Note: app.js is the entry point of our application.

Install Dependencies

We need to install packages using for our API. The packages are

Express JS: Express JS is the most popular framework which is used in node.js.

Mongoose: Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.

body-parser: package that can be used to handle JSON requests.

Config: Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables, command line parameters, or external sources.

I’ve used -save option to save all dependencies in the package.json file. Open the package.json file looks like this -

Initializing the Server:

Create a new file, let’s name it is app.js directly inside the node-restful-crud-app directory

Open the app.js file and require all the dependencies we previously installed.

// app.js

const express = require(‘express’);

const bodyParser = require(‘body-parser’);

// initialize our express app

const app = express();

Finally, We listen port number 4000 for incoming connections.

Inside node-restful-crud-app directory, I will create following the four sub directories:

We will be working with a design pattern called MVC(Model, View, Controller). Note: In this tutorial we didn’t use view concept and will do later.

App Configuration

I like to keep all configuration files for the app in a separate folder. Let’s create config folder in the root directory.

Now, create a default.json inside app/config folder with the following contents -

// bg_mongo is a test database.

{

“DBHost”: “mongodb://localhost:27017/bg_mongo”

}

Connection to the Database

I like to keep all the db related files in a separate folder. Let’s create models folder in the root directory.

Now, create db.js file inside app/models folder with following contents -

Now, create product.model.js file inside app/models folder with following contents -

Routes:

Create a new folder called routes inside the app folder.

Now, create a new file called product.route.js inside app/routes folder with the following contents -

Note that We have added a require statement for product.controller.js file. We’ll define the controller file in the next section. The controller will contain methods for handling all the CRUD operations.

Handling CRUD Operations:

Create a new folder called controllers inside the app folder.

Now, create a new file called product.controller.js inside app/controllers folder with the following contents -

Before doing crud operations, we have to require product.model.js file in this controller.

Retrieve all products:

Retrieve single product:

Create new product:

Update a product:

Delete a product:

Postman:

Postman is a HTTP client that is used for testing, documenting and the development of APIs.

Please see the below screenshots which we are tested this App through postman.

/Get method — All items

Image for post
Image for post

/Get method — single item

Image for post
Image for post

/POST method — create

Image for post
Image for post

/PUT method — update item

Image for post
Image for post

/DELETE method — delete item

Image for post
Image for post

Read article about Creating a RESTful API with HapiJS and NodeJS and MYSQL to click here.

Comments

comments

Originally published at http://www.phpexpertise.com on May 16, 2020.

Written by

I’m a blogger. Love to share technical tips for web development. My official website phpexpertise.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store