ERR_CONNECTION_RESET: Ошибка, когда я пытаюсь загрузить большие файлы, используя node.js / multer, развернутый на эластичном beanstalk -nginx Server - PullRequest
0 голосов
/ 31 декабря 2018

Когда я пытаюсь загрузить большие файлы (zip-100mb +), я получаю ошибки тайм-аута ERR_CONNECTION_RESET.Я использую multer с node.js, а внешний интерфейс - angular6.Мое соединение медленное и загрузка занимает много времени, обычно ошибка появляется через 2-3 минуты в процессе загрузки!

Это размещено в эластичном бобовом стебле - 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');
                            });
                    }
                });
            }
        });
    });
}

1 Ответ

0 голосов
/ 31 декабря 2018

Вы должны изменить nginx конфигурацию в /etc/nginx/nginx.conf и изменить это:

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

, затем просто перезапустите службу sudo service restart nginx

...