如何从 Azure Functions return HTML 内容

How to return HTML content from Azure Function

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

文件目录

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>

您可以使用 context.res = res; 而不是 context.done(null, res);

像这样:

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