Azure 函数 nodejs return 200 OK,响应为空
Azure function nodejs return 200 OK with empty response
我正在使用 Azure 函数做一些工作,一切都很好,只是我无法从结果中获取响应正文:
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const fetch = require('node-fetch');
const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));
fetch(myURL)
.then(data => {
if (!data.ok) {
throw new Error('some error occurred');
}
return data;
})
.then(data => data.text())
.then(text =>
context.res = {
body: text //here is the problem
});
}
function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
修复
空响应与使用没有 await
的 async
方法有关
所以只需删除异步或使用异步等待。
请不要使用lambda表达式,你需要这样执行,然后会有结果(不确定你在做什么,但我认为你的设计有问题):
const fetch = require('node-fetch');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));
fetch(myURL)
.then(
//some logic here.
context.res = {
body: "This is a test." //here is the problem
}
);
}
增强版async/await,@BowmanZhu
的版本
const fetch = require('node-fetch');
module.exports = async function (context, req) {
try{
context.log('JavaScript HTTP trigger function processed a request.');
const myURL= (req.query.apiURL || (req.body && req.body.apiURL)),
fetchResp = await fetch(myURL),
resBody =fetchResp.text();
/** TODO LOGIC **/
context.res = {
status:200,
body: resBody
};
}catch(err){
context.res = {
status:500,
body: err.message
};
}
}
我正在使用 Azure 函数做一些工作,一切都很好,只是我无法从结果中获取响应正文:
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const fetch = require('node-fetch');
const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));
fetch(myURL)
.then(data => {
if (!data.ok) {
throw new Error('some error occurred');
}
return data;
})
.then(data => data.text())
.then(text =>
context.res = {
body: text //here is the problem
});
}
function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
修复
空响应与使用没有 await
的 async
方法有关
所以只需删除异步或使用异步等待。
请不要使用lambda表达式,你需要这样执行,然后会有结果(不确定你在做什么,但我认为你的设计有问题):
const fetch = require('node-fetch');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const myURL= (req.query.apiURL|| (req.body && req.body.apiURL));
fetch(myURL)
.then(
//some logic here.
context.res = {
body: "This is a test." //here is the problem
}
);
}
增强版async/await,@BowmanZhu
的版本 const fetch = require('node-fetch');
module.exports = async function (context, req) {
try{
context.log('JavaScript HTTP trigger function processed a request.');
const myURL= (req.query.apiURL || (req.body && req.body.apiURL)),
fetchResp = await fetch(myURL),
resBody =fetchResp.text();
/** TODO LOGIC **/
context.res = {
status:200,
body: resBody
};
}catch(err){
context.res = {
status:500,
body: err.message
};
}
}