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:
- Uniform Interface
- Stateless
- Cacheable
- Client-Server
- Layered System
- Code on Demand (optional)
RESTful services use HTTP requests to perform CRUD (Create, Read, Update, Delete) operations.
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.
- Ultra-fast I/O
- Asynchronous and single threaded
- MVC like structure
- Robust API makes routing easy
- Node.js
- MongoDB
- Text editor (Notepad++, Sublime, Atom, VSCode)
- Postman
Node.js and MongoDB should be installed. If you haven’t installed them, you can install from the below URLs.
Basically this project contains RESTful APIs for CRUD operations which developed using Mongoose and Express.js.
Open your terminal and follow the following steps.
-
Clone this project to your computer
git clone https://github.com/osandadeshan/expressjs-restful-apis-demo.git
-
Navigate to the project folder
cd expressjs-restful-apis-demo
-
Install the relevant node modules
npm install
-
Start the server
npm start
Then you will see
RESTful API demo server started on: 3000
Now that everything is now connected, let’s test each of the routes and the respective methods.
Open your postman and type:
-
http://localhost:3000/tasks in the enter request URL section
-
Change the HTTP method to POST and select raw radio button
-
Then choose JSON (application/json)
-
Enter the body as follows
{
"name": "Task 1",
"category": "R&D",
"status": "Completed",
"createdDate": "2019-07-11T04:06:44.110Z"
}
-
Click on Send button
-
It will give the response as 201 (Created)
- You can try other HTTP methods such as GET, PUT and DELETE as well using this postman collection
Note: Health route can be verified using GET http://localhost:3000/health Health Request