Azure Function 不通过 context.res 返回响应

Que*_*wer 1 javascript azure node.js azure-functions

我试图从 Azure Function 返回一个 JSON 对象,我的意思是,通过context.res执行响应创建根本不起作用的示例。

context.res = {
   body: {"name": "JSON STATHAM"}, //No. No mistake.
   contentType: 'application/json'
};
Run Code Online (Sandbox Code Playgroud)

为什么?

只有通过context.done作为第二个参数传递时它才起作用。

mat*_*ewc 5

查看您的 http 输出绑定name属性是如何指定的。有一段时间,我们的模板/示例默认使用$return输出绑定名称。使用$return这意味着响应应该是函数的返回值

{
    "bindings": [
        {
            "type": "httpTrigger",
            "name": "req",
            "direction": "in",
            "methods": [ "get" ]
        },
        {
            "type": "http",
            "name": "$return",
            "direction": "out"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在该模式下,只有通过返回的值context.done(即函数返回值)才会被使用。更改$return为您选择的其他名称即可使用context.res

  • 我希望它能得到更好的记录。谢谢@mathewc。 (3认同)