我知道我可以使用这样的url参数:
"myfunction?p=one&p2=two"
并成为
的代码request.query.p = "one";
但我更喜欢这样(快速路由风格):
"myfunction/:myvalue"
并使用此网址:
/myfunction/nine
在代码中变成了这个:
request.params.myvalue = "nine"
但我似乎无法找到如何在Azure Functions,任何想法或文档中配置路径路径?
答案 0 :(得分:9)
我们在Azure Functions中为HTTP触发器提供了路由支持。您现在可以添加遵循ASP.NET Web API路由命名语法的路由属性。 (您可以通过Function.json或通过门户网站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示例:
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();
}
<击> 今天,您必须使用Azure API Management等服务来自定义路线。
正在进行PR以向Azure Functions本身添加自定义路由。该团队希望它能在下一个版本中登陆。 https://github.com/Azure/azure-webjobs-sdk-script/pull/490
击>
注意:我是Azure Functions团队的PM
答案 1 :(得分:4)
使用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:随意为该项目做出贡献!