为什么对Node服务器的此请求挂起? [英] Why does this request to Node server hang?

查看:116
本文介绍了为什么对Node服务器的此请求挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常想表达和结识.我试图使用多方和此处提供的代码上传图片. 我检查了文件大小.当我上传的文件大小超过限制时,它会出现在问题部分"中.问题是服务器仅在请求超时后挂起并作出响应.我已经尝试了许多关于堆栈溢出的解决方案,但似乎没有任何效果.如果文件大小小于限制,它将起作用.我非常确定代码到达问题部分,并且上传逻辑没有问题.但是似乎我必须在问题部分"中做一些事情.请告诉我我在想什么.

I am very new to express and node. I was trying to upload an image using multiparty and code given here. I have put a check for file size. When I upload a file of size greater than the limit it lands in the "problem section". The problem is the server hangs and responds back only after request timeout. I have tried many solution on stack overflow but nothing seems to work. It works if the file size is below the limit. I am very sure that the code reaches the problem section and there is no problem with the upload logic. But it seems that I have to do something in the "problem section". Please tell me what am I missing.

我已将问题部分中的代码替换为 next(),res.send(),res.end(),next(err),返回;但它不起作用.无论如何挂起.

I have replaced the code in the problem section with next(), res.send(), res.end(), next(err), return; but It does not work. It hangs no matter what.

以下是代码:

router.post("/image", function(req, res, next) {
  if(req.user) {
    upload.uploadToS3(req, S3_BUCKET, S3_PROFILE_IMAGE_FOLDER, function(result) {
      if(result.error != null && result.error === false) {
        models.Customer.update({
          orignalImage : result.fileLocation
        },{
          where : { mobileNumber : req.user.mobileNumber}
        }).then(function(customer) {
          if(customer) {
            res.send({
              url: result.fileLocation,
              error : false
            });
          } else {
            res.status(400);
            res.send({error : true,
              error_message : 'Image upload failed'});
          }
        });
      } else {
        //PROBLEM SECTION
        res.status(404);
        res.json({error : true, error_message : result.error_message});
      }
    });
  } else {
    res.status(403);
    res.send({message: "access denied"});
  }
});

超时后的响应

如果您需要更多详细信息,请告诉我,我会上传. var uploadToS3 = function(req,S3_BUCKET,folder,callback){ var s3Client = knox.createClient({ 安全:错误, 密钥:awsConfig.accessKeyId, 机密:awsConfig.secretAccessKey, 铲斗:S3_BUCKET, });

Please tell me if you need more details I will upload it. var uploadToS3 = function(req, S3_BUCKET, folder, callback) { var s3Client = knox.createClient({ secure: false, key: awsConfig.accessKeyId, secret: awsConfig.secretAccessKey, bucket: S3_BUCKET, });

  var headers = {
    'x-amz-acl': 'public-read',
  };

  var form = new multiparty.Form();
  var batch = new Batch();

  batch.push(function(cb) {
    form.on('part', function(part) {
      var validity = validateFile({type : part.headers['content-type'], name : part.filename, length : part.byteCount});
      console.log(validity);
      if(validity.isValid) {
        cb(null, { filename : folder+"/"+generateFileName({name : part.filename}), part : part});  
      } else {
        cb({error : true, error_message : validity.reason, part:part }, "advra kedavra");
      }
    });
  });

  batch.end(function(err, results) {

    if (err) {
      console.log(err);
      err.statusCode = 200;
      callback(err);
    } else {
      form.removeListener('close', onEnd);
      var result = results[0];
      var part = result.part;
      var destPath = result.filename;
      var counter = new ByteCounter();
      part.pipe(counter); // need this until knox upgrades to streams2
      headers['Content-Length'] = part.byteCount;
      s3Client.putStream(part, destPath, headers, function(err, s3Response) {
        result = {};
        if(err) {
          console.log(err);
          result.error = true;
          result.error_message = "Problem in uploading!";
        } else {
          console.log(s3Response.req.url);
          result = {error: false, statusCode : s3Response.statusCode, message : "file upload successful.", fileLocation : s3Response.req.url};
        }   
        callback(result);
      });
      part.on('end', function() {
        console.log("File upload complete", counter.bytes);
      });
    }
  });
  function onEnd() {
    console.log("no uploaded file");
    callback({error:false, error_message:"no uploaded file."});
  }
  form.on('error', function(err) {
    console.log('Error parsing form: ' + err.stack);
  });
  form.on('close', onEnd);
  form.parse(req);
}

推荐答案

经过3天的搜索,我找到了一个答案. Express.js关闭响应

After a 3 day long search for the answer I found one answer. Express.js close response

问题部分应为以下内容:

The problem section should be the following :

res.status(400);
res.set("Connection", "close");
res.json({error:true, error_message : "<some - error message>"});

这篇关于为什么对Node服务器的此请求挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆