【发布时间】: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