Replies: 5 comments
-
then you can use
|
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
We dont have a use case for
|
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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
default
export 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,Beta Was this translation helpful? Give feedback.
All reactions