RESTful API Development using Express.js

API CRUD operation development using Express.js


Express.js

Contents

  1. What is REST?
  2. What is Express.js?
  3. Express.js Architecture
  4. Advantages of Express.js
  5. Tools/Technologies
  6. Pre-requisites
  7. Getting started
  8. Setting up the server
  9. Setting up the schema
  10. Setting up the routes
  11. Setting up the controller
  12. Completing the server
  13. Adding a middleware
  14. Testing via Postman
  15. Github source

1. What is REST?

REST — Representational State Transfer

REST, or Representational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. REST-compliant systems, often called RESTful systems, are characterized by how they are stateless and separate the concerns of client and server. We will go into what these terms mean and why they are beneficial characteristics for services on the Web.

The REST architectural style describes six constraints that were originally communicated by Roy Fielding in his doctoral dissertation and defines the basis of RESTful-style as:

1. Uniform Interface

2. Stateless

3. Cacheable

4. Client-Server

5. Layered System

6. Code on Demand (optional)

RESTful services use HTTP requests to perform CRUD (Create, Read, Update, Delete) operations.


2. What is Express.js?

Express is a fast, assertive, essential and moderate web framework of Node.js. You can assume express as a layer built on the top of the Node.js that helps manage a server and routes. It provides a robust set of features to develop web and mobile applications.

Let’s see some of the core features of Express framework:

· It can be used to design single-page, multi-page and hybrid web applications.

· It allows to setup middleware to respond to HTTP Requests.

· It defines a routing table which is used to perform different actions based on HTTP method and URL.

· It allows to dynamically render HTML Pages based on passing arguments to templates.


3. Express.js Architecture

Express.js architecture

4. Advantages of Express.js

· Ultra-fast I/O

· Asynchronous and single threaded

· MVC like structure

· Robust API makes routing easy


5. Tools/Technologies

· Node.js

· MongoDB

· Text editor (Notepad++, Sublime, Atom, VSCode)

· Postman


6. Pre-requisites

Node.js and MongoDB should be installed. If you haven’t installed them, you can install from the below URLs.

Node.js

MongoDB


7. Getting started

In this tutorial, I will guide you to develop RESTful APIs for CRUD operations using Mongoose and Express.js. Basically, you will be able to develop routes for GET, POST, PUT and DELETE HTTP methods.

Open your terminal and kindly follow the following steps.

  1. Create a folder for your project. Here I will name it as “expressjs-restful-apis-demo”
mkdir expressjs-restful-apis-demo

2. Navigate to that folder

cd expressjs-restful-apis-demo

3. Create a “package.json” file — This package.json file provides the information of the project and its dependencies

npm init

4. Press Enter to complete the creation of package.json

5. Add the below dependencies to your package.json

Dependencies

6. Update your package.json with the following

package.json

7. Create a file named “server.js” — In this file, we will be writing the protocols to create our server

8. Create a folder called “api”

mkdir api

Inside this folder called api, create three separate folders called “models”, “routes”, and “controllers” by executing

mkdir api/controllers api/models api/routes

9. Create “tasksController.js” in the api/controllers folder, “tasksRoutes.js” in the api/routes folder, and “tasksModel.js” in the api/models folder

10. Our folder structure should look like this now

Package structure of Express.js

8. Setting up the server

1. Let’s install express and nodemon, express will be used to create the server while nodemon will help us to keep track of changes to our application by watching changed files and automatically restart the server

npm install — save-dev nodemonnpm install express –save

2. Then we can install express-healthcheck, which can be used to check the health of the server

npm install express-healthcheck

3. Open the server.js file and type/copy the code below into it

server.js

4. On your terminal, execute

npm start

This will start the server and then you will see

RESTful API demo server started on: 3000


9. Setting up the schema

First, we need to install mongoose. Mongoose is what we will use to interact with a MongoDB(Database) instance.

npm install mongoose –save

After installation, open the tasksModel.js file in your api/models folder and type the following code into the file and save.

tasksModel.js

From the code above, we are defining the set of attributes for our MongoDB collection. Simply this is the payload we need to use to create a task from the service.

As you can see, it the task collection(table) will contain a name: a string, a category: a string and the date it was created. It also contains task status which we have defined as pending — a default value for every task created.


10. Setting up the routes

Routing refers to determining how an application responds to a client request for a specific endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

Each of our routes has different route handler functions, which are executed when the route is matched.

Below we have defined two basic routes(‘/tasks’, and ‘/tasks/taskId’) with different methods

/tasks’ has to methods(GET and POST), while ‘/tasks/taskId’ has GET, PUT and DELETE.

As you can see, we required the controller so each of the routes methods can call it’s respective handler function.

To do this, open the tasksRoutes.js file in the route folder and paste the code snippet below into.

tasksRoutes.js

11. Setting up the controller

Open tasksController.js file with your text editor (VSCode, Sublime, Atom e.t.c) and let’s deep dive into coding.

In this controller, we would be writing 5 different functions namely: getAllTasks, createTask, getTaskById, editTaskById, deleteTaskById. We will export each of the functions for us to use in our routes.

Each of these functions uses different mongoose methods such as find, findById, findOneAndUpdate, save and remove.

tasksController.js

12. Completing the server

Earlie, we had a minimal code for our server to be up and running in the server.js file.

In this section we will be connecting our handlers(controllers), database, the created models, body parser and the created routes together.

Open the server.js file created a while ago and follow the following steps to put everything together.

Essentially, you will be replacing the code in your server.js with the code snippet from this section

1. Connect your database by adding a url to the mongoose instance connection

2. Load the created model — task

3. Install bodyParser and use bodyParser Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

It exposes various factories to create middlewares. All middlewares will populate the req.bodyproperty with the parsed body, or an empty object ({}) if there was no body to parse (or an error was returned)

4. Register the created routes in the server

server.js

5. Start MongoDB server

Open your terminal and run

mongod

6. Start Node server

Open your terminal and run

npm start

13. Adding a middleware

Having done all these, what happens if we entered a wrong route? say you entered ‘http://localhost:3000/task', It responds with a message “Cannot GET /task”. Let’s add express middleware which could be used to return more interactive messages.

Middlewares basically intercepts incoming HTTP request and as such you can use them to perform several operations ranging from authentication to validations etc.

To do this, open your server.js file and paste the code snippet into it.

server.js

The snippet above helps to redirect and respond whenever a wrong route is entered on the site.


14. Testing via Postman

Now that everything is now connected, let’s test each of the routes and the respective methods.

Open your postman and type:

1. http://localhost:3000/tasks in the enter request URL section

2. Change the HTTP method to POST and select raw radio button

3. Then choose JSON (application/json)

4. Enter the body as follows

JSON payload

5. Click on Send button

6. It will give the response as 201 (Created)

POST Request

POST request

7. You can try other HTTP methods such as GET, PUT and DELETE as well using this postman collection

GET Request

GET request

PUT Request

PUT request

DELETE Request

DELETE request

Note: Health route can be verified using GET http://localhost:3000/health

Health Request

Health request

Osanda Deshan Nimalarathna

Written by

Founder of MaxSoft | FOSS Contributor | Framework Developer | Senior Software Automation Engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade