Azure函数:NodeJS - HTTP响应呈现为XML而不是HTML

时间:2017-02-07 10:21:16

标签: node.js azure azure-functions

我有一个带有function.json的节点azure函数,如下所示:

{
  "disabled": false,
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [ "get" ]
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}

我想让函数返回html给我一个这样的页面: enter image description here

但是,当我像这样编写index.js时:

module.exports = function (context, sentimentTable) {
    context.res = {
        body: "<!DOCTYPE html> <html> <head> </head> <body> Hello World </body> </html>",
        contentType: "text/html"
    };

    context.done();
};

我得到了这个:

enter image description here

Azure函数可以返回html吗?

2 个答案:

答案 0 :(得分:6)

必须是&#39;内容类型&#39;并以这种方式指定标题

context.res = {
    body: '...',
    headers: {
        'Content-Type': 'text/html; charset=utf-8'
    }
}

在AzureServerless.com上查看此博客帖子 - http://azureserverless.com/2016/11/12/a-html-nanoserver/

答案 1 :(得分:2)

或者,您可以使用流利的表达方式:

module.exports = function (context, req) {
    context.res
        .type("text/html")
        .set("someHeader", "someValue")
        .send("<!DOCTYPE html> <html> <head> </head> <body> Hello World </body> </html>");
};

确保http输出绑定设置为res而不是$return