Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle 405 #3513

Closed
wangfengming opened this issue Dec 19, 2017 · 9 comments
Closed

handle 405 #3513

wangfengming opened this issue Dec 19, 2017 · 9 comments

Comments

@wangfengming
Copy link

wangfengming commented Dec 19, 2017

How to handle 405?
Here is a simple way:

insert the code on https://github.com/expressjs/express/blob/master/lib/router/index.js#L253

        if (method !== 'OPTIONS') req.notAllowedRoute = route

then I can handle 405 by:

const notAllowedHandler = (req, res, next) => {
    if (req.notAllowedRoute) {
        res.header('Allow', Object.keys(req.notAllowedRoute.methods).join(', ').toUpperCase())
        res.status(405)
    } else {
        next()
    }
}
@wesleytodd
Copy link
Member

Hi @wangfengming. Does this conversation help you? pillarjs/router#62

@wangfengming
Copy link
Author

wangfengming commented Dec 20, 2017

It means I need to handle 405 per every route?

@wesleytodd
Copy link
Member

Currently yes. The solution you propose is rather complicated for the router to make assumptions about because of the shear number of combinations the current api supports. Away from my computer right now, but I can try to outline some of what I mean when I get back to one.

@wangfengming
Copy link
Author

wangfengming commented Dec 20, 2017

If Router can store the "only matched path route" and/or "matched path and method route" on req, then I can handle 405 with minimal code.

Sorry for my poor english. 😂

@wesleytodd
Copy link
Member

Your english is not poor 😀

Later tonight Ill write some examples of edge cases and why I think it is not that simple.

@wangfengming
Copy link
Author

It seems that pillarjs/router#34 and pillarjs/router#56 are on the same topic. 🤔

@wesleytodd
Copy link
Member

I think both of these are generic avenues for achieving the end goal you want. If we go one of those directions then it supersedes this issue. @wangfengming maybe you could comment over there to put pressure on moving those forward?

@gajananpp
Copy link

PR #2703 is already opened which allows router and route to be configured for automatically responding with status 405 and is added to 4.17 milestone 👍

@dougwilson dougwilson added this to the 4.17 milestone Oct 27, 2018
@dougwilson dougwilson mentioned this issue Oct 27, 2018
23 tasks
@dougwilson dougwilson removed this from the 4.17 milestone May 10, 2019
@mastermatt
Copy link

I'm going to close this as it seems to be answered/resolved and hasn't had any activity in over a year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants