Jamie Dixon
Jamie Dixon

Reputation: 4302

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?

Upvotes: 1

Views: 1409

Answers (2)

Matt Mason
Matt Mason

Reputation: 2724

Alternatively, you can use the fluent express style:

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

Make sure that the http output binding is set to res and not $return.

Upvotes: 3

Steve Lee
Steve Lee

Reputation: 6121

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/

Upvotes: 7

Related Questions