【问题标题】:How can I do Routing in Azure Functions?如何在 Azure Functions 中进行路由?
【发布时间】:2016-09-10 20:24:48
【问题描述】:

我知道我可以像这样使用 url 参数:

"myfunction?p=one&p2=two"

在代码中变成

request.query.p = "one";

但我更喜欢这样(表达路由风格):

"myfunction/:myvalue"

并使用此网址:

/myfunction/nine

在代码中变成这样:

request.params.myvalue = "nine"

但我似乎找不到如何在 Azure Functions 中配置这样的路由路径,有什么想法或文档吗?

【问题讨论】:

    标签: node.js azure routing azure-functions


    【解决方案1】:

    我们已经为 Azure Functions 中的 HTTP 触发器提供了路由支持。您现在可以添加遵循 ASP.NET Web API 路由命名语法的路由属性。 (您可以直接通过 Function.json 或门户 UX 进行设置)

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

    函数.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 示例:

    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 示例:

    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();
    }
    

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


    今天,您必须使用 Azure API 管理之类的服务来自定义您的路线。

    正在进行 PR 以将自定义路由添加到 Azure Functions 本身。该团队希望它将在下一个版本中登陆。 https://github.com/Azure/azure-webjobs-sdk-script/pull/490


    注意:我是 Azure Functions 团队的 PM

    【讨论】:

    • 有没有办法改变默认的“函数名”?所以我想创建一个代表restful服务/api的url,然后提供一个swagger定义。所以每个函数处理一个单独的资源。再加上没有自定义路由加上无法更改functionname(我假设),没有办法基于Azure函数创建Restful api?
    • 您可以在创建函数时设置名称。如果您更改磁盘上的文件夹名称,您也可以更改函数名称。您可以通过 Kudu(在功能应用程序设置菜单上)执行此操作。
    • @ChrisAnderson-MSFT 在您指定的路线中 guid 但在您期望的功能中 int? 。那是错字吗?天蓝色功能路由是否支持guid 属性。我遇到了一些错误。,所以链接stackoverflow.com/questions/46743180/…
    • 对于 node.js - 希望你能坚持使用节点社区的习惯 - 表达类似路由。 req/res 对象的这种上下文包装使事情变得不那么熟悉并且更难使用所有可用的中间件
    【解决方案2】:

    azure-function-express

    通过azure-function-express,您可以使用所有expressjs 功能,包括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);
    

    查看更多示例 here,包括处理 all routes within a single Azure Function handler 的方法。

    PS:请随意为该项目做出贡献!

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2016-10-19
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多