[英]How to extend AdonisJS Response class?
当用户在我的RESTful应用程序中创建帖子时,我想将响应状态代码设置为201。
我遵循文档 start/hooks.js
如下所示创建了start/hooks.js
:
'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)
})
})
现在在我的PostController.js中,我有这个:
async store( {request, response, auth} ) {
const user = await auth.current.user
response.sendStatus(201)
}
但是我在这个端点上得到了500个HTTP代码。 我究竟做错了什么?
我注意到,当我运行Response.hasMacro('sendStatus')
我得到了false 。
实际上,adonis已针对所有响应代码开箱即用...
只需编写response.created(.....)。
您还可以使用例如:.badRequest()、. notFound()等...有关更多信息: https ://adonisjs.com/docs/4.1/response#_descriptive_methods
我昨天解决了这个问题:
hooks.after.httpServer(() => {
const Response = use('Adonis/Src/Response')
Response.macro('sendStatus', function (status) => {
this.status(status).send(status)
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.