2

我有一个处理文件上传的节点 js/Express 应用程序。当我使用 CURL 上传文件时,它工作正常。但是当我尝试使用Postman上传时,我得到错误状态 503 服务不可用。在服务器日志中它说H12 error Request timeout这只是意味着应用程序需要一段时间来响应请求(无论是状态 200 还是呈现 json) 。这里解释。我怀疑处理文件的功能可能需要很长时间?但是为什么 CURL 可以工作而不是 Postman。当我从另一个应用程序上传文件时,我也会得到相同的行为。有人可以指导我如何解决这个问题吗?谢谢提前。这是我处理上传的功能。

router.route('/images')    
.post (function(req, res) {

var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
    console.log("Uploading: " + (filename)); 
    fstream = fs.createWriteStream(__dirname + '/public/img/'+ filename);
    file.pipe(fstream);
    fstream.on('close', function () {
    res.status(200); //Seems like we don't reach this point that's why the server timesout
    res.json({ message: 'File uploaded' });
        
    });
   });
});

Postman 生成的代码片段

POST /api/images HTTP/1.1
Host: myapp.herokuapp.com
Cache-Control: no-cache
Postman-Token: 31dbdf6f-7ff5-ad21-e42e-67b32f72b51c
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=""; filename=""
Content-Type: 


----WebKitFormBoundary7MA4YWxkTrZu0gW

更新

我刚刚意识到req.busboy.on('file', function (fieldname, file, filename)根本没有被调用。可能出了什么问题?

4

0 回答 0