REST is "Representational State
Transfer", big name but let me break it down for you in
simple terms. Over internet when two systems need to interface with each other
they need some form/language to communicate, REST is that medium which enables
transferring of data between two remote systems. REST is based on HTTP protocol
and hence supports all the HTTP verbs like GET, POST, PUT, DELETE, PATH etc. We
also have SOAP (Simple Object Access Protocol ) & WSDL (Web Service Definition Language) web services which are still used in many places but
they are old and creepy. SOAP is based on XML format and is slow whereas with
REST get the freedom to use formats like text, XML or JSON. REST takes the
advantage of HTTP protocol because of which it is more faster and with json
format which is faster to parse and compact as compared to XML.
REST stands for REpresentational
State Transfer and API stands for Application Program Interface. REST is a
software architectural style that defines the set of rules to be used for
creating web services. Web services that follow the REST architectural style
are known as RESTful web services. It allows requesting systems to access and
manipulate web resources by using a uniform and predefined set of rules.
Interaction in REST-based systems happen through Internet’s Hypertext Transfer
Protocol (HTTP).
A Restful system consists of a:
- client who requests for the resources.
- server who has the resources.
It is important to create REST API
according to industry standards which results in ease of development and
increase client adoption.
· REST
is based on the resource or noun instead of action or verb based. It means that
a URI of a REST API should always end with a noun. Example: /api/customers is a
good example, but /api?type=customers is a bad example of creating a REST API.
· HTTP
verbs are used to identify the action. Some of the HTTP verbs are – GET, PUT,
POST, DELETE, UPDATE, PATCH.
· A
web application should be organized into resources like users and then uses
HTTP verbs like – GET, PUT, POST, DELETE to modify those resources. And as a
developer, it should be clear that what needs to be done just by looking at the
endpoint and HTTP method used.
URI
|
HTTP
VERB
|
DESCRIPTION
|
api/customers
|
GET
|
Get all customers
|
api/customers
|
POST
|
Add a customer
|
api/customers/1
|
PUT
|
Update a customer with id = 1
|
api/customers/1
|
DELETE
|
Delete a customer with id = 1
|
api/customers/1
|
GET
|
Get a customer with id = 1
|
· Always
use plurals in URL to keep an API URI consistent throughout the application.
· Send
a proper HTTP code to indicate a success or error status.
Steps to create
RESTful Services with Express
1. create a new Folder ExpressApp
2. npm init --y // initialize package.json // --y
is flag to set all package.json fields to default
3. npm i express // install express
4. Create a file name index.js
//First load express module using Code
const express = require('express');
const app = express();
app.get('/',(req,res) => {
res.send('hello world');
// Call back function is called route handler
});
app.listen(3000,()=>{
console.log('Server is Running on 3000....');
});
5. Now we create an application having Basic CRUD Operations using RESTFul Services with Express having HTTP methods.
HTTP Methods
app.get();
app.post();
app.put();
app.deletet();