Skip to content

handle 405 #3513

@wangfengming

Description

@wangfengming

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions