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

v4 allow registering separate http methods to the same route #98

Open
abaudhuin opened this issue May 31, 2023 · 3 comments
Open

v4 allow registering separate http methods to the same route #98

abaudhuin opened this issue May 31, 2023 · 3 comments

Comments

@abaudhuin
Copy link

Hello,

With v4 nodejs typescript, I'm trying to create two functions with the same name but different methods. Like this:

app.get("users", {
  authLevel: "anonymous",
  handler: getUser,
});

app.patch("users", {
  authLevel: "anonymous",
  handler: patchUser,
});

I would expect to have 2 functions running.
But when running locally with func start I get only one.

Here is the logs:

Azure Functions Core Tools
Core Tools Version:       4.0.5198 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.21.1.20667

[2023-05-31T12:35:16.018Z] Worker process started and initialized.

Functions:

   users: [PATCH] http://localhost:7071/api/users

For detailed output, run func with --verbose flag.
[2023-05-31T12:35:20.977Z] Host lock lease acquired by instance ID '00000000000000000000000023AC63E5'.

It looks like the second one is overriding the first one.

I could use app.http(...) but it would be far easier if the express like way if doing things was working out of the box.
Do you know of some way I could achieve this ?

Related information

Version: 4.0.0-alpha.10
OS: Linux

@ejizba
Copy link
Contributor

ejizba commented May 31, 2023

For now this isn't supported, but we'll keep it open as a feature request. The recommended workaround is to use app.http(...) as you mentioned.

For true support, we would need a fix on the host. I don't remember the exact error, but I had a prototype that allowed this on the Node.js side but the host was still throwing errors. It doesn't allow two functions to affect one route.

We might be able to hack something together on the Node.js side if we collect the separate method functions and pass them to the host as one function. The main downside is the settings (like authLevel) would need to match.

Related to #62.

@ejizba ejizba added the feature label May 31, 2023
@ejizba ejizba changed the title v4 multiple functions with the same name are overriding each other v4 allow registering separate http methods to the same route May 31, 2023
@ejizba ejizba added this to the Backlog Candidates milestone Jun 1, 2023
@ejizba ejizba added the P2 label Oct 10, 2023
@restfulhead
Copy link

restfulhead commented Dec 24, 2023

Maybe obvious, but another workaround is to use different names and specify the same route:

app.get('get-users', {
    route: 'users',
    authLevel: 'anonymous',
    handler: getUsers
});
app.post('post-users', {
    route: 'users',
    authLevel: 'anonymous',
    handler: postUsers
});

@ihnaqi
Copy link

ihnaqi commented Mar 23, 2024

Since you have different handlers to deal with each of the request, adding methods array will work and you know which handler to call based on the method being called via the request object. I would have done something like this.

app.http('users', {
   route: "<if/{any}>",
   methods: ["GET", "PATCH"],
   authLevel: 'anonymous',
   handler: async (req, context) {
      switch (req.method) {
         case "GET":
            return getHandler
         case "PATCH":
            return patchHandler  
      }
   }
})

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

4 participants