简体   繁体   English

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

[英]Error serving HTML files from an Azure function

I am trying to open, read and return an HTML files using Azure functions. 我正在尝试使用Azure函数打开,读取和返回HTML文件。 I am developing locally and the logs says that the function executed successfully however on the browser I am getting 500 internal server error. 我在本地开发,日志显示该功能已成功执行,但是在浏览器上却出现500内部服务器错误。 Am I doing something wrong in here? 我在这里做错什么了吗?

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

};

Note I am sure that 404.html exists and index.html exists. 注意我确定404.html存在并且index.html存在。 When I log the contents of htmlContent it is giving the correct output. 当我记录htmlContent的内容时,它给出了正确的输出。

functions.json functions.json

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "methods":["get"],
      "route":"home",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Response on Chrome 在Chrome上的回应

在Chrome上显示“ Content-Length”

If I removed "Content-Length" header the status code changes to 406. 如果删除了“ Content-Length”标头,则状态码将更改为406。

在Chrome上没有“内容长度”的响应

Update 1 The code seems to be running normally on Azure Portal but it is not working when running it locally. 更新1该代码似乎在Azure门户上正常运行,但在本地运行时不起作用。

It looks like you are combining two methods of returning data from an http triggered function( context.res and context.done() ): https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#accessing-the-request-and-response 看来您正在组合从http触发函数返回数据的两种方法( context.rescontext.done() ): https : //docs.microsoft.com/zh-cn/azure/azure-functions/functions-参考节点#访问请求和响应

Since you are using context.res , try removing context.done(); 由于您使用的是context.res ,请尝试删除context.done();

You are making an incorrect use of context.res , you shouldn't be overwriting it but instead leveraging the methods provided by the Response class provided in the Azure NodeJS worker. 您对context.res使用不正确,不应覆盖它,而应利用Azure NodeJS辅助程序中提供的Response类提供的方法。 If you are using using VSCode you'll get intellisense for these methods. 如果您使用的是VSCode,则这些方法将获得智能感知。 Otherwise see: https://github.com/Azure/azure-functions-nodejs-worker/blob/dev/src/http/Response.ts 否则请参阅: https : //github.com/Azure/azure-functions-nodejs-worker/blob/dev/src/http/Response.ts

Your code should look something like this instead. 您的代码应该看起来像这样。

context.res.setHeader('content-type', 'text/html; charset=utf-8')
context.res.raw(htmlContent)

Using context.res.raw or context.res.send will already perform the context.done call for you. 使用context.res.rawcontext.res.send将已经为您执行context.done调用。

Make sure you use content-type=text/html; charset-utf8 确保使用content-type=text/html; charset-utf8 content-type=text/html; charset-utf8 instead of content-type=text/html or you'll trigger an issue with the returned content-type. content-type=text/html; charset-utf8代替content-type=text/html否则您将触发返回的content-type问题。 Instead of returning content-type=text/html you end up getting content-type=text/plain which will fail to render your html. 而不是返回content-type=text/html您最终会得到content-type=text/plain ,这将无法呈现您的html。

Addressed on: https://github.com/Azure/azure-webjobs-sdk-script/issues/2053 地址在: https : //github.com/Azure/azure-webjobs-sdk-script/issues/2053

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM