Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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 触发http时函数超时_Javascript_Node.js_Azure Functions_Jimp - Fatal编程技术网

Javascript 触发http时函数超时

Javascript 触发http时函数超时,javascript,node.js,azure-functions,jimp,Javascript,Node.js,Azure Functions,Jimp,我做错了什么?为什么功能超时? 我有一个blob触发功能,它工作得非常好: const Jimp = require("jimp"); module.exports = function(context, myBlob) { const correlation = context.bindings.inputBlob.correlation; const inputImage = context.bindings.inputBlob.image; const imageName =

我做错了什么?为什么功能超时?

我有一个blob触发功能,它工作得非常好:

const Jimp = require("jimp");

module.exports = function(context, myBlob) {
  const correlation = context.bindings.inputBlob.correlation;
  const inputImage = context.bindings.inputBlob.image;
  const imageName = context.bindings.inputBlob.imageName;

  context.log(
    correlation + "Attempting to convert this image to a tiff: " + imageName
  ); 
  Jimp.read(Buffer.from(inputImage, "base64"), function(err, image) {
    image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
      const response = {
        myimage: tiff.toString("base64"),
        correlation: correlation
      };
      context.bindings.outputBlob = response
      context.log(
        correlation + "Successfully converted " + imageName + " to tiff."
      );
      context.done();
    });
  });
};
以下是function.json文件:

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "images-in/{destination}/{name}",
      "connection": "AZURE_STORAGE_CONNECTION_STRING"
    },
    {
      "name": "inputBlob",
      "type": "blob",
      "direction": "in",
      "path": "images-in/{destination}/{name}",
      "connection": "AZURE_STORAGE_CONNECTION_STRING"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "{destination}/{name}.tiff",
      "connection": "AZURE_STORAGE_CONNECTION_STRING",
      "direction": "out"
    }
  ],
  "disabled": false
}
工作方式:

  • 将水滴放入特定位置
  • 将生成输出blob 通过另一个位置的功能(整个过程不超过5秒) 我决定需要http触发该函数,因此做了以下更改:

    const Jimp = require("jimp");
    
    module.exports = function(context, req) {
    
      Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {
        image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
          const response = {
            myimage: tiff.toString("base64"),
            correlation: "yeeeeees"
          };
    
    
        });
          context.res = {
              body: response
          };
          context.done();
      });
    
    
    };
    
    function.json

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ]
    }
    
    然而,我得到了500分:

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ]
    }
    

    遥测显示如下:

    我做错了什么?为什么功能超时?

    我对函数进行了以下更新:

    const Jimp = require("jimp");
    
    module.exports = function (context, req) {
        const text = Buffer.from(req.body, "base64").toString("utf-8");
    
        Jimp.read(text, function(err, image) {
        if (err) {
          context.res = {
            body: err
          };
          context.done();
        }
    
        image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
          if (error) {
            context.res = {
              body: error
            };
            context.done();
          }
          const response = {
            myimage: tiff.toString("base64"),
            correlation: "yeeeeees"
          };
          context.res = {
            body: response
          };
          context.done();
        });
      });
    };
    
    这产生了以下荒谬的反应:

    {
      "errno": -4058,
      "code": "ENOENT",
      "syscall": "open",
      "path": "D:\\home\\site\\wwwroot\\$R\u0005������{\u001av��r��Ū�O�$z�ނ)",
      "methodName": "constructor"
    }
    

    如果您在Azure中检查node.js日志,您会看到类似“未定义响应”的错误,因为您定义响应的范围与使用响应的范围不同

    所以基本上你不能调用context.done();函数,这就是请求引发超时异常的原因

    使用async/await将帮助您避免此类问题。请检查此代码示例以了解可能出现的错误

    const Jimp = require("jimp");
    
    module.exports = function(context, req) {
    
      Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {
    
        if(err){
          context.res = {
              body: err
          };
          context.done();
        }
    
        image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
          if(error){
            context.res = {
              body: error
            };
            context.done();
          }
          const response = {
            myimage: tiff.toString("base64"),
            correlation: "yeeeeees"
          };
          context.res = {
            body: response 
          };
          context.done();
        });
      });
    };
    

    您的第二个代码对
    response
    (超出范围)的引用无效,并且您调用
    done
    的时间太短。应该放在内部回调中(稍后执行)。可能这不是代码的正确表示形式?谢谢!我已经尝试使用下面答案中的代码,但是,在使用您的代码后,我仍然得到超时,我得到以下响应
    {“methodName”:“constructor”}
    非常奇怪!这是日志的屏幕截图,您是否尝试在本地使用相同的代码和相同的图像样本?也许你可以在你的本地计算机上调试它的错误。