Adonis rest – basic component of restful API based on Adonis JS



The basic component of restful API based on adonisjs, adonisjs Chinese website:…

Adonis rest - basic component of restful API based on Adonis JS


  1. cnpm install --save adonis-rest

get ready

Tips: please make sure your/app/Model/There are some model files in the directory. If not, you can use them./ace make:model NewsTo create a news model


Route. Put ('/ API /: resource','restcontroller. Update ') // optional
Route.resource('/api/:resource', 'RestController')

Create /app/Http/Controllers/RestController.js

'use strict'

const BaseRestController = require('adonis-rest')

class RestController  extends BaseRestController{


module.exports = RestController

In this way, the restful interfaces of all models can be accessed.


Base uri: http://localhost:3333/api

Request method URL describe
GET /news Get news list
POST /news Get a news
PUT/PATCH /news/:id Modify a news item
DELETE /news/:id Delete a news item

GET /news

Get all news

URL query parameters

Parameter name Example value describe
pagination 1 Get the data containing paging information, otherwise get the flat array, but you can still useheadersTo get paging information
query {“user_id”: 1} Query criteria can also be usedfilterorwhere
page 1 Current page
perPage 10 The quantity displayed on each page can also be usedlimit
offset 10 Skip quantity can also be usedskip
fields id,title,created_at selectField of
orderBy id Sorting, you can use-idexpressdesc(in descending order)

Paging information field in headers

  • X-Pagination-Total-Count

  • X-Pagination-Page-Count

  • X-Pagination-Current-Page

  • X-Pagination-Per-Page

queryFormat of field

  • query={"created_at": "2017-07-07"}

  • query={"created_at": [">", "2017-07-07"]}or<,<>wait

  • query={"created_at": ["between", ["2017-07-01", "2017-07-31"]]}ornotBetween

  • query={"user_id": ["in", [1,2,3] ]}ornotIn

  • query={"user_id": ["raw", 'user_id IS NULL' ]}

something the matter?

stayIssuesTell me inside