Replies: 5 comments
then you can use |
|
Thanks for the very nice code snippet @KieraDOG! I like that setup for a user-land solution, however, how would that implement multiple methods for a single endpoint like GET, POST, PUT for |
|
We dont have a use case for
|
|
Ah, I was just throwing out a random example, sorry. I was looking for handling multiple methods like fetching a list of users (GET /users) and adding a new user (POST /users) on a single api file. |
|
I found this tiny helper library to help with this, which works super great with API routes: module.exports = methods({
get: async (req, res) => {
return res.status(200).end('It's a GET request!');
},
post: async (req, res) => {
return res.status(200).end('It's a POST request!');
}
})Works well with middlewares too! module.exports = someMiddlewareForAllMethods(methods({
get: async (req, res) => {
return res.status(200).end('It's a GET request!');
},
post: someMiddleWareJustForPostMethod(async (req, res) => {
return res.status(200).end('It's a POST request!');
})
}))But I still think it would be great if Next.js did this out-of-the-box by checking the |
Uh oh!
There was an error while loading. Please reload this page.
Feature request
Is your feature request related to a problem? Please describe.
Handling various Methods in API routes is tiresome. If I only want to handle POST, I have to do something like
If I want to handle multiple methods I either have to inline them all or break them into separate functions.
Describe the solution you'd like
I like having the
defaultexport as a generic request handler. Expanding on that, it would be nice to have named exports for the various Methods supported. So my above example would become,All reactions