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.   



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.