How can I do routing in Azure?

I know that I can use URL parameters, for example:

"myfunction?p=one&p2=two"

and in the code that becomes

request.query.p = "one";

but I would rather get it like this (expressing the routing style):

"myfunction/:myvalue"

and use this url:

/myfunction/nine

which becomes in code:

request.params.myvalue = "nine"

but I can’t find how to set up a route route, such as in Azure Functions, any ideas or documents?

+4
source share
2 answers

We sent route support for HTTP triggers to Azure Functions. Now you can add a route property that follows the ASP.NET Web API route naming syntax. (You can install it directly through Function.json or through the UX portal)

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

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


, Azure API Management, .

PR Azure. , . https://github.com/Azure/azure-webjobs-sdk-script/pull/490 a >


: PM Azure Functions

+7

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

, Azure.

PS: !

+4

Source: https://habr.com/ru/post/1654234/


All Articles