How can I do Routing in Azure Functions?

前端 未结 2 727
不知归路
不知归路 2021-02-05 21:08

I know that I can use url parameters like this:

\"myfunction?p=one&p2=two\"

and in code that becomes

request.query.p = \"o         


        
相关标签:
2条回答
  • 2021-02-05 21:31

    We've shipped route support for HTTP Triggers in Azure Functions. You can now add a route property which follows ASP.NET Web API route naming syntax. (You can set it directly via the Function.json or via the portal UX)

    "route": "node/products/{category:alpha}/{id:guid}"

    Function.json:

    {
        "bindings": [
            {
                "type": "httpTrigger",
                "name": "req",
                "direction": "in",
                "methods": [ "post", "put" ],
                "route": "node/products/{category:alpha}/{id:guid}"
            },
            {
                "type": "http",
                "name": "$return",
                "direction": "out"
            },
            {
                "type": "blob",
                "name": "product",
                "direction": "out",
                "path": "samples-output/{category}/{id}"
            }
        ]
    }
    

    .NET sample:

    public static Task<HttpResponseMessage> Run(HttpRequestMessage request, string category, int? id, 
                                                    TraceWriter log)
    {
        if (id == null)
           return  req.CreateResponse(HttpStatusCode.OK, $"All {category} items were requested.");
        else
           return  req.CreateResponse(HttpStatusCode.OK, $"{category} item with id = {id} has been requested.");
    }
    

    NodeJS sample:

    module.exports = function (context, req) {
    
        var category = context.bindingData.category;
        var id = context.bindingData.id;
    
        if (!id) {
            context.res = {
                // status: 200, /* Defaults to 200 */
                body: "All " + category + " items were requested."
            };
        }
        else {
            context.res = {
                // status: 200, /* Defaults to 200 */
                body: category + " item with id = " + id + " was requested."
            };
        }
    
        context.done();
    }
    

    Official docs: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#url-to-trigger-the-function


    Today, you'd have to use a service like Azure API Management to customize your routes.

    There is a PR in progress to add custom routes to Azure Functions itself. The team is hoping it will land in the next release. https://github.com/Azure/azure-webjobs-sdk-script/pull/490


    Note: I'm a PM on the Azure Functions team

    0 讨论(0)
  • 2021-02-05 21:35

    azure-function-express

    With azure-function-express you can use all expressjs functionalities including routing ;)

    const createHandler = require("azure-function-express").createAzureFunctionHandler;
    const express = require("express");
    
    // Create express app as usual
    const app = express();
    app.get("/api/:foo/:bar", (req, res) => {
      res.json({
        foo  : req.params.foo,
        bar  : req.params.bar
      });
    });
    
    // Binds the express app to an Azure Function handler
    module.exports = createHandler(app);
    

    See more examples here including a way to handle all routes within a single Azure Function handler.

    PS: Feel free to contribute to the project!

    0 讨论(0)
提交回复
热议问题