Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Azure函数提供HTML文件时出错_Javascript_Node.js_Azure_Azure Functions - Fatal编程技术网

Javascript 从Azure函数提供HTML文件时出错

Javascript 从Azure函数提供HTML文件时出错,javascript,node.js,azure,azure-functions,Javascript,Node.js,Azure,Azure Functions,我正在尝试使用Azure函数打开、读取和返回HTML文件。我正在本地开发,日志显示该功能已成功执行,但在浏览器上我收到500个内部服务器错误。我在这里做错什么了吗 const fs = require('fs'); const path = require('path'); const mime = require('../node_modules/mime-types'); module.exports = function (context, req) { const staticF

我正在尝试使用Azure函数打开、读取和返回HTML文件。我正在本地开发,日志显示该功能已成功执行,但在浏览器上我收到500个内部服务器错误。我在这里做错什么了吗

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);
                }
            })
    }

};
铬的响应

如果我删除了“内容长度”标题,状态代码将更改为406


更新1该代码似乎在Azure Portal上正常运行,但在本地运行时不起作用。

看起来您正在结合从http触发函数返回数据的两种方法(
context.res
context.done()
):


由于您正在使用
context.res
,请尝试删除
context.done()

看起来您正在组合从http触发函数返回数据的两种方法(
context.res
context.done()
):


由于您正在使用
context.res
,请尝试删除
context.done()

您不正确地使用了
context.res
,您不应该覆盖它,而是应该利用Azure NodeJS worker中提供的响应类提供的方法。如果您使用的是VSCode,您将获得这些方法的intellisense。否则,请参见:

您的代码应该是这样的

context.res.setHeader('content-type', 'text/html; charset=utf-8')
context.res.raw(htmlContent)
使用
context.res.raw
context.res.send
将已经为您执行
context.done
呼叫

确保使用
content-type=text/html;charset-utf8
而不是
content-type=text/html
,否则将触发返回内容类型的问题。不是返回
content-type=text/html
,而是得到
content-type=text/plain
,这将无法呈现html


地址:

您不正确地使用了
context.res
,您不应该覆盖它,而是应该利用Azure NodeJS worker中提供的响应类提供的方法。如果您使用的是VSCode,您将获得这些方法的intellisense。否则,请参见:

您的代码应该是这样的

context.res.setHeader('content-type', 'text/html; charset=utf-8')
context.res.raw(htmlContent)
使用
context.res.raw
context.res.send
将已经为您执行
context.done
呼叫

确保使用
content-type=text/html;charset-utf8
而不是
content-type=text/html
,否则将触发返回内容类型的问题。不是返回
content-type=text/html
,而是得到
content-type=text/plain
,这将无法呈现html


地址:

谢谢,但删除上下文。完成();使函数无限期运行。我还尝试使用context.done(null,res)并将输出绑定中的“name”属性设置为“$return”,得到了与我在问题中所述相同的结果。谢谢,但删除context.done();使函数无限期运行。我还尝试使用context.done(null,res)并将输出绑定中的“name”属性设置为“$return”,我得到了与我在问题中所述相同的结果。只需尝试您的代码,它对我来说效果很好(本地/azure)。您是否尝试将其部署到azure?仅在azure门户上进行了尝试,效果良好。但是由于某些原因,它仍然不能在本地工作。“index.html”内容是什么?本地错误是否有消息(chrome“网络”工具中的“响应”选项卡)?从CreateReact应用程序生成的HTML文件。我将标题中的内容类型设置修改为基于发送的文件。我现在将更新这个问题。另外,很遗憾,本地错误在响应选项卡中没有任何内容。值得一提的是,如果我删除了headers对象,我就可以在浏览器中看到HTML文件的内容。只需尝试一下你的代码,它对我很好(本地/azure)。您是否尝试将其部署到azure?仅在azure门户上进行了尝试,效果良好。但是由于某些原因,它仍然不能在本地工作。“index.html”内容是什么?本地错误是否有消息(chrome“网络”工具中的“响应”选项卡)?从CreateReact应用程序生成的HTML文件。我将标题中的内容类型设置修改为基于发送的文件。我现在将更新这个问题。另外,很遗憾,本地错误在响应选项卡中没有任何内容。值得一提的是,如果我删除了headers对象,我就能在浏览器中看到HTML文件的内容。