Koa.js
There are 5 constraints
Uniform interface
Stateless
Cacheable
Client-Server
Layered System
1.Uniform interface
URIs refer to distinct services based on the http standard. HTTP requests consist of a header, body, and query parameters, whereas URI responses consist of a header, status, and body. verbs in HTTP (GET ,PUT ,POST ,DELETE)
Example: Create faculties
Insert new resource -
POST /faculties
Fetch a resource -
GET /faculties
Replace existing resources
PUT / faculties / {faculty id}
Delete resource
DELETE /faculties/{faculty id}
Update resource
PATCH /faculties/{faculty id}
Versioning of Rest APIs is also crucial. because it will serve in the long run when newer API versions are available Versioning is a type of text used to distinguish URIs.
As a example:
POST v1 /faculties
GET v1 /faculties
PUT v1/faculties /{faculty id}
Query parameters
In the REST API, query arguments are valid situations. Query parameters are optional parameters that are used to indicate which operations on the operation are optional. Multiple query parameters are possible.
Example: filter faculties order by name
GET v1/faculties? order-by = name
2.Stateless
A rest API is what Stateless is. They would not be allowed to carry any state. In the server, there is no client state.
Every request must be self-contained. It means that the request should include everything necessary for the request You’ll need some data that you've previously created or stored to develop this one. Assume that authentication is enabled.
Build an authentication, verify the user's identity, and then create a server session to handle the next request.
3.Client -Server
Clients interact with servers without understanding where they are or what they do.
To make the call, all you need is the URI or URL, and the communication platform is the HTTP stack.
HTTP is a protocol that sits on top of TCP. It solves practically all of the problems in the client server design, such as packet looser and stack-based ordering.
The server will have an interface, and the client will rely on it.
Clients might be any number of different people. It's a lightweight client. There is no link between the two. It is dependent on the server's interface. The server is self-contained. It has the ability to scale independently. It can alter on its own.
It may be a cloud one day and then the Google Platform the next. However, using URIs, HTTP and the HTTP stack will fix the problem. Client-server architecture is what it means.
4.Cacheable
In the REST API, homogeneity as well as statelessness regulate Cacheable. Caching is possible for all get APIs.
Account balance, for example, will not be cached due to business restrictions rather than technological constraints on caching a rest resource.
However, there may be a business limitation.
Clients can cache resources via implicit caching.
Explicit caching means that the server decides what and how to cache.
5.Layered System
A layered system emphasizes the complexity of an API, which may include several separate APIs. High scalability In a microservices system, a request can be to gather data from many services, combine it, and return it.
However, as a customer, we are unaware of any technological or architectural challenges.
Client interacts with a specific interface, which is an HTTP-specific URI-based interface.
The client knows nothing else. If there are any breaking changes, the server will release a newer version, such as v2. However, the client will continue to use version 1. Multiple layers and intermediary services can reside between the client and the server since the server is sufficiently detached.
Comments
Post a Comment