简体   繁体   中英

How to change http exception in adonisjs?

I am newbie in adonisjs. I want to implement custom response if route method not match.

I have route like this

Route.post('/create', function * (request, response) {response.send('success')})

when call url /create with GET in browser, It send respond 404 not found. Can I use custom response with 405 method not allowed?

The Adonisjs not use request and response like expressjs, you need deconstruct the object.

Your route will running with that code:

Route.post('/create', ({ request, response }) => { response.send('success') })

or

Route.post('/create', (ctx) => { ctx.response.send('success') })

I just found out the simplest method. Just put this at the bottom of all the routes Route.get('*', ({ view }) => view.render('errorPage'))

It will check all the routes from the top, reaches the bottom and hits the view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM