如何编写将使用Express API的Azure函数 [英] How to write azure functions which will use express api
问题描述
我有天蓝色的功能, 在index.js中,我有以下代码
I have a azure function, In index.js i have the following code
module.exports = function (context, req) {
const createHandler = require('azure-function-express').createHandler;
const app = require('express')();
app.get("/home", (req, res) => {
const y = { "name": "name", "dob": "ddmmyyyy" }
context.res = y
context.done()
});
module.exports = createHandler(app);
context.done();
};
我有function.json:
i have function.json :
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "{*segments}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false
}
我的azure函数中有上述文件,但是如果我按了api端点,我将无法获得任何输出,只是空白页. 我必须使用express来处理许多其他端点,而azure函数中是否有任何处理方法.
i have the above files in my azure function but i am not able get any output i just a blank page if i hit the api end point. i have to use express to handle many other end points is there any way to handle in azure functions.
当我使用Node.js本地应用程序设置时,我能够在单个模块中使用express并处理许多api端点,这在azure函数中是可能的吗?或者我必须为每个端点使用不同的功能
when i use nodejs local application setup, i am able to use express and handle many api end points with in a single module is that possible in azure functions? or i have to use different functions for each end point
推荐答案
请参见下面的代码.我们可以将Azure功能用作通用快递应用程序.
See code below. We can use Azure function as a common express app.
const createHandler = require('azure-function-express').createHandler;
const app = require('express')();
app.get("/home", (req, res) => {
res.json({ "name": "name", "dob": "ddmmyyyy" });
});
app.get("/work", (req, res) => {
res.json({ "name": req.query.name, "dob": "ddmmyyyy" });
});
module.exports = createHandler(app);
如果使用azure-function-express
,则
module.exports = function (context, req)
和context.done()
不再有用.如果要使用其他上下文方法,请改用req.context.请参见 azure-function-express模块文档.
module.exports = function (context, req)
and context.done()
are no longer useful if azure-function-express
is in use. If you want to use other method of context, use req.context instead. See azure-function-express module doc.
此外,Azure函数默认在路由中具有前缀"api",如果不需要它(如上面的代码),请将其更改为空host.json
.
Besides, Azure function has a prefix "api" in route by default, if you don't need it(like code above), change it to empty your host.json
.
如果您的函数运行时为〜2(测试版).
If your function runtime is ~2(beta).
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
}
}
其他〜1
{
"http": {
"routePrefix": ""
}
}
这篇关于如何编写将使用Express API的Azure函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!