Reputation: 22794
When a user creates a post in my RESTful application, I want to set the response status code to 201.
I followed the documentation and created start/hooks.js
as follows:
'use strict'
const { hooks } = require('@adonisjs/ignitor')
hooks.after.httpServer(() => {
const Response = use('Adonis/Src/Response')
Response.macro('sendStatus', (status) => {
this.status(status).send(status)
})
})
Now in my PostController.js, I have this:
async store( {request, response, auth} ) {
const user = await auth.current.user
response.sendStatus(201)
}
But I am getting 500 HTTP code at this endpoint. What am I doing wrong?
I noticed when I run Response.hasMacro('sendStatus')
I get false.
Upvotes: 0
Views: 756
Reputation: 1499
In fact adonis already have this out of the box for all response codes...
Just write response.created(.....).
You can also use for example: .badRequest(), .notFound(), etc... More info on: https://adonisjs.com/docs/4.1/response#_descriptive_methods
Upvotes: 1
Reputation: 22794
I solved this problem yesterday:
hooks.after.httpServer(() => {
const Response = use('Adonis/Src/Response')
Response.macro('sendStatus', function (status) => {
this.status(status).send(status)
})
})
Upvotes: 0