ADVERTISEMENT
ADVERTISEMENT

Request and response in AdonisJs

Here, we are going to learn about the Request and response in AdonisJs.
Submitted by Radib Kar, on January 19, 2021

In our last tutorial, we got started with the idea of the controller and found how we can route a request to a controller and how controller methods handle the request.

Well, this tutorial is focused to discuss details on request & response. Thought we will keep things in the context of Adonis.js, but this tutorial will serve the purpose of general request & response.

At the very beginning of this entire Adonis.js series, we discussed that the ultimate motive is to make a backend for a CRUD application & through Adonis.js we will create APIs that will create our independent backend. So, for any API it's all about request & response.

There will be endpoints where you will send requests for some data & as a response, your backend will provide you with the data in some suitable format(JSON preferably) via that endpoint. This is the basic concept that lies behind the REST API.

So, now we will look for different kind of requests that we can send towards our API

Probably you already know GET request. There are few more requests which are quite often used for CRUD APIs

  1. GET: to fetch all data
  2. GET/:id: to fetch a particular data with id = id
  3. PUT: send data to create an entry (request type: POST)
  4. PATCH: update an entry(request type: POST)
  5. DELETE: delete an entry

These are the five API endpoints we need for any CRUD object. For example, for the project table,

We need to have privilege for the below operations

  1. Create a project (PUT request)
  2. Delete (DELETE request)
  3. Update (PATCH request)
  4. Show all projects (GET request)
  5. Show a project(GET/:id request)

Show if we want to create a project,

We need to send a PUT request.

Now let's come to each request individually

PUT request is to create a project, so to create a project we will of course have some data to be sent. Now we can't send this data as params as it will be exposed then. That's why PUT request has a body where the data would be sent.

This is similar for PATCH request which updates an already created project.

GET request is very simple which will fetch the list of projects

GET/:id will fetch only the project with id =id

DELETE will delete the project with id =id

Now coming towards the response part,

We should have implementation for routing each of the requests towards the correct controller. Then specific methods of the controller will handle the requests and will send back the response. This response will be rendered in the front-end application.

Okay, that's it for this tutorial. In our next tutorial, we will walk through postman which is an application to test API endpoints. We will create all the API endpoints as discussed here & will send request to our server and will find response back.   

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.