【问题标题】:How to return HTML content from Azure Function如何从 Azure Function 返回 HTML 内容
【发布时间】:2021-05-25 08:45:42
【问题描述】:

Azure 函数运行,但是没有 HTML 输出?如何在 localhost 上获取 HTML 内容?

文件目录

  • HttpTrigger1
    • index.html
    • index.js
    • function.json
    • sample.dat

index.js

const fs = require('fs');
const path = require('path');

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    
    var res = {
        body: "",
        headers: {
            "Content-Type": "text/html"
        }
    };

    if (req.query.name || (req.body && req.body.name)) {
        // Just return some HTML
        res.body = "<h1>Hello " + (req.query.name || req.body.name) + "</h1>";

        context.done(null, res);
    } else {
        // Read an HTML file in the directory and return the contents
        fs.readFile(path.resolve(__dirname, 'index.html'), 'UTF-8', (err, htmlContent) => {
            res.body= htmlContent;
            context.done(null, res);
        });
    }
}

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>hello world</title>
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

【问题讨论】:

    标签: javascript azure azure-functions


    【解决方案1】:

    您可以使用context.res = res; 代替context.done(null, res);

    像这样:

    请求函数端点后,我们可以看到如下截图的结果页面:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2018-10-29
      • 1970-01-01
      • 2018-03-17
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多