ERR_CONNECTION_RESET:错误当我尝试使用node.js / multer上传大文件时,部署在弹性beanstalk -nginx服务器上

问题描述 投票:1回答:1

当我尝试上传大文件(zip-100mb +)时,我遇到了超时错误ERR_CONNECTION_RESET。我正在使用带有node.js的multer,而前端是angular6。我的连接速度很慢,上传需要花费很多时间,通常情况下,上传过程需要2-3分钟才会出现错误!

这是在弹性beanstalk - nginx服务器托管

错误回复:

{
"errors": 
         {
         "message": "Not Found",
         "error": {}
         }
}

码:

router.post('/file_upload/:id', verify_token, (httpReq, httpRes) => {
    httpRes.setTimeout(4800000, function(){ // 40 minute timeout adjust for larger uploads
        console.log('Request has timed out.');
            httpRes.send(408);
        });
    let tempFileName = '';
    let tempMimeType = '';

    // Define temporary storage on disk for file
    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, 'public/temp/');
        },
        filename: function (req, file, cb) {
            let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
            tempFileName = Date.now() + ext;
            cb(null, tempFileName);
        }
    });

    // Define upload action and validations
    const upload = multer({
        storage: storage,
        limits: { fileSize: 1024 * 1024 * 1024 },
        fileFilter: function (req, file, cb) {
            tempMimeType = file.mimetype;
            if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/jpg' && file.mimetype !== 'model/x.stl-binary' && file.mimetype !== 'application/zip' && file.mimetype !== 'application/x-7z-compressed' && file.mimetype !== 'application/vnd.rar') {
                req.fileValidationError = 'Invalid file format';
                return cb(null, false, new Error('Unsupported file format'));
            }
            cb(null, true);
        }
    }).single('patientFile');

    // Upload file
    upload(httpReq, httpRes, function(err) {
        if(httpReq.fileValidationError) {
            return httpRes.end({success: false, message: httpReq.fileValidationError});

        }

        if (err && err.code === 'LIMIT_FILE_SIZE') {
            httpRes.status(400).json({success: false, message: err.message});
            console.log(err.message);
        }

        uploadAttachment(httpReq.params.id, {
            fileName: tempFileName,
            description: httpReq.body.description,
            mimeType: tempMimeType
        }).then(data => {
            httpRes.status(200).json({success: true, message: 'File uploaded successfully'})
        }).catch(error => {
            httpRes.status(400).json({success: false, message: error.message});
            console.log(err.message);
        });
    });

});

/*
* Uploads the attachment to couchdb record of the associated patient
*
* @param    patientId   The patient Id
* @param    fileData    The file to upload  {fileName: '', description: '', mimeType: ''}
*
* */
function uploadAttachment(patientId, fileData) {
    return new Promise((resolve, reject) => {
        couch_db.get(patientId, (err, doc)=> {
            if (!err) {
                let existingPatientRecords = {};
                if (doc.hasOwnProperty('patientRecords')) {
                    existingPatientRecords = doc['patientRecords'];
                }
                const recordsData = {};

                recordsData['patientRecords'] = existingPatientRecords;
                recordsData['patientRecords'][fileData.fileName] = fileData.description;

                couch_db.merge(patientId, recordsData, (err, res) => {
                    if (!err) {
                        const viewUrl = '_design/patient/_view/by_id';
                        const queryOptions = {
                            key: patientId
                        };

                        let documentRevision = '';

                        couch.get(dbName, viewUrl, queryOptions).then(
                            function(data, headers, status){
                                let selectedPatient = {};

                                if (data.data['rows'].length > 0 && data.data['rows'][0].hasOwnProperty('value')) {
                                    selectedPatient = data.data['rows'][0].value;
                                    documentRevision = selectedPatient['rev'];
                                    if(documentRevision) {
                                        var idAndRevData = {
                                            id: selectedPatient['id'],
                                            rev: selectedPatient['rev']
                                        };

                                        var attachmentData = {
                                            name: fileData.fileName,
                                            'Content-Type': fileData.mimeType
                                        };

                                        try {
                                            var readStream = fs.createReadStream(`public/temp/${fileData.fileName}`);
                                            var writeStream  = couch_db.saveAttachment(idAndRevData, attachmentData, (body) =>{
                                                fs.unlinkSync(`public/temp/${fileData.fileName}`);
                                                resolve();
                                            });
                                            readStream.pipe(writeStream)
                                        } catch(err) {
                                            reject(err);
                                            console.log('error1');
                                        }
                                    }
                                } else {
                                    reject({error: 'NO_DATA_FOUND', message: 'No data found for the given patientId'});
                                }
                            }, (err) => {
                                reject(err);
                                console.log('error2');
                            });
                    }
                });
            }
        });
    });
}
node.js nginx file-upload multer connection-reset
1个回答
1
投票

你应该改变nginx中的/etc/nginx/nginx.conf配置并改变这个:

http {
  ...
  client_max_body_size 128m; #Any desired size in MB
  ...
}

然后只需重新启动服务sudo service restart nginx

© www.soinside.com 2019 - 2024. All rights reserved.