我想用Azure函数打开,读取并返回一个HTML文件。我正在开发本地和日志说,该功能成功执行,但在浏览器中,我得到500内部服务器错误。我在这里做错了什么?从Azure功能提供HTML文件时出错
const fs = require('fs');
const path = require('path');
const mime = require('../node_modules/mime-types');
module.exports = function (context, req) {
const staticFilesFolder = 'www/build/';
const defaultPage = 'index.html';
getFile(context, req.query.file);
function getFile(context, file) {
const homeLocation = process.env["HOME"];
if(!file || file == null || file === undefined){
context.done(null,{status:200,body:"<h1>Define a file</h1>",headers:{
"Content-Type":" text/html; charset=utf-8"
}});
}
fs.readFile(path.resolve(path.join(homeLocation, staticFilesFolder, file)),
(err, htmlContent) => {
if (err) {
getFile(context, "404.html");
}
else {
const res = {
status: 200,
body: htmlContent,
headers:{
"Content-Type": mime.lookup(path.join(homeLocation, staticFilesFolder, file))
}
}
context.done(null,res);
}
})
}
};
注意 我相信404.html存在和index.html存在。当我登录htmlContent
的内容时,它会给出正确的输出。如果我删除 “内容长度” 在Chrome
functions.json
响应头的状态代码变为406
更新1代码似乎在Azure门户上正常运行,但在本地运行时无法正常工作。
只是想你的代码,它工作正常,我(在本地/天蓝色)。你有没有尝试将它部署到天蓝色? –
刚刚在Azure Portal上试用过它,它工作正常。但是由于某些原因,它仍然不在本地工作。 –
什么是“index.html”内容?本地错误是否包含消息(chrome“网络”工具中的响应标签)? –