Jamie Dixon Jamie Dixon - 1 year ago 52
Node.js Question

Azure Functions: NodeJS - HTTP Response Renders as XML rather than HTML

I have a node azure function with the function.json like this:

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


I want the function to return html to give me a page like this:
enter image description here

However, when I write the index.js like this:

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

context.done();
};


I am getting this:

enter image description here

Can Azure Functions return html?

Answer Source

Must be 'Content-Type' and you specify headers this way

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

See this blog post on AzureServerless.com - http://azureserverless.com/2016/11/12/a-html-nanoserver/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download