2

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?

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
Arwani Ali
  • 41
  • 4
  • Define a catch-all route and return a custom response – MilkyWayJoe Aug 30 '16 at 14:02
  • thank a lot, but I still confused how to do that. Can you give me an example? – Arwani Ali Aug 30 '16 at 14:36
  • @arwaniAli Checkout http://adonisjs.com/docs/3.1/error-and-exceptions#_catching_exception. The idea is to listen for exceptions thrown during the request lifecycle and same can be done inside `app/Listeners/Http.js` inside `handleError` method – Aman Virk Nov 23 '16 at 13:03

2 Answers2

1

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') })

Jason Rabelo
  • 119
  • 4
1

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

Lokesh Bajracharya
  • 457
  • 2
  • 8
  • 19