gpt4 book ai didi

node.js - ERR_CONNECTION_RESET : Error When i try to upload large files using node. js/multer ,部署在 elastic beanstalk -nginx 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:32 26 4
gpt4 key购买 nike

当我尝试上传大文件(zip- 100mb+)时,我收到超时错误ERR_CONNECTION_RESET。我将 multer 与 node.js 一起使用,前端是 angular6。我的连接速度很慢,上传需要很长时间,通常上传过程 2-3 分钟后就会出现错误!

这托管在 elastic 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');
});
}
});
}
});
});
}

最佳答案

您应该更改 /etc/nginx/nginx.conf 中的 nginx 配置并更改此:

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

然后重启服务sudo service restart nginx

关于node.js - ERR_CONNECTION_RESET : Error When i try to upload large files using node. js/multer ,部署在 elastic beanstalk -nginx 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com