azure-function-express⚡️Allows Express.js usage with Azure Functions

联合创作 · 2023-09-27 12:37


azure-function-express



Allows Express usage with Azure Function



npm version Node Node Node Travis Status Coverage Status MIT licensed


Description


Connect your Express application to an Azure Function handler, and make seamless usage of all middlewares you are already familiar with.


Usage


In your index.js:



const createHandler = require("azure-function-express").createHandler;
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);


Make sure you are binding req and res in your function.json:



{
"bindings": [{
"authLevel" : "anonymous",
"type" : "httpTrigger",
"direction" : "in",
"name" : "req",
"route" : "foo/{bar}/{id}"
}, {
"type" : "http",
"direction" : "out",
"name" : "res"
}]
}


To allow Express handles all HTTP routes itself you may set a glob star route in a single root function.json:



{
"bindings": [{
"authLevel" : "anonymous",
"type" : "httpTrigger",
"direction" : "in",
"name" : "req",
"route" : "{*segments}"
}, {
"type" : "http",
"direction" : "out",
"name" : "res"
}]
}


Note that segments is not used and could be anything. See Azure Function documentation.


All examples here.


Context


All native Azure Functions context properties, except done, are exposed through req.context.


As en example, you can log using:



app.get("/api/hello-world", (req, res) => {
req.context.log({ hello: "world" });
...
});


Runtime compatibility


Supported Node version are:



Azure Functions runtime v1 and v2 beta are both supported.


License


Apache 2.0 © Yves Merlicco

浏览 17
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报