Next.js Discord

Discord Forum

Route Handlers Next.js 13: catch all request methods

Unanswered
Flamey posted this in #help-forum
Open in Discord
In Next.js 13, you need to export a function for every request method. I was wondering how I could also make another function that would catch all methods that don't have a function and return 405. Thanks in advance!

10 Replies

That’s the default behavior of route handlers, you don’t need to manually do that
I don’t think so. What do you need from the response? If it’s the allow methods, there should be an allow header on the 405 response
@Flamey okay but could i customize the response
make a handleNotAllowedMethod: (request: Request) => Response and export it several times like export { handleNotAllowedMethod as PUT, handleNotAllowedMethod as DELETE }, but as baked said, there isn't any reason to do this; nextjs already sends an empty 405 response by default
for every req method possible?
'cause yea what i wanted to do was have a 405 code but with this response body
{
    res: {
        code: 405,
        status: "405 Method Not Allowed",
        info: `Method ${req.method} is not allowed on this endpoint.`
    }
}
to have it be the same as every other error response that i have in my api
but yea ok thanks :3
Not a big fan of that api response design personally. All of that data that’s already known when just reading the response code. No need to await and parse the body