3

I am working on a aplication that was written in express 3, now we upgraded to express 4, and i have a problem whith file uploads. Everything works on my local computer mac OSX, but doesn't work on production server on ubuntu.

i am uploading zip files, with png and psd inside.

I am uploading file by multipart form: with multer middleware.

Locally everything is ok, but on production envoriment the connection is breaking. File starts uploading to the main upload root, but as a part of a chunk, for example i upload 50 mb zip file, but uploaded is about 30 kb and the connection is break, because the onFileDataupload event that upload data as chunks was break.

settings in app.js

my body parser settings:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }) );

my multer settings:

app.use(multer({
dest: './upload',
limits: {
fieldNameSize: 500,
files: 2,
fields: 20,
fileSize: 200 * 1024 * 1024
},
rename: function (fieldname, filename) {
  return fieldname + filename + Date.now();
},
onFileUploadStart: function (file) {
 console.log('Upload starting for filename: ' + file.originalname);
},
onFileUploadData: function (file, data) {
  // console.log(data.length + ' of ' + file.fieldname + ' arrived')
},
onParseStart: function () {
  console.log('Form parsing started at: ', new Date())
},
onParseEnd: function (req, next) {
 console.log('Form parsing completed at: ', new Date());
  // usage example: custom body parse
  //req.body = require('qs').parse(req.body);
  // call the next middleware
  next();
},
onFileUploadComplete: function (file) {
  console.log(file.fieldname + ' uploaded to ' + file.path);
},
onFileSizeLimit: function (file) {
  console.log('Failed: ', file.originalname)
  fs.unlink('./' + file.path) // delete the partially written file
},
onFilesLimit: function () {
  console.log('Crossed file limit!')
},
onFieldsLimit: function () {
  console.log('Crossed fields limit!')
},
onPartsLimit: function () {
  console.log('Crossed parts limit!')
},
onError: function(error, next) {
  console.log("Error occurred while uploading the file!!");
  next(error);
  } 

}));

i tried also formidable as alternative, and i have the same problem, works locally, dont work on production envoriment. And i think that can be a problem with requests managin by node. On production envoriment we use pm2. Smoething is breaking the connection while file uploading.

Did anyone have maby a similar problem and find a resolution? Thanks

1 Answers1

7

You probably have pm2 with "watch" option enabled; and upload files inside watched directory. Then pm2 restarts application every time you start writing new files (because of code reloading).

To check if that solves Your's issue, just drop "watch": true from jour process.json or remove flag --wath from command line. If it's working, read about pm2 watch ignores here: https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#watch--restart, and configure it properly for your env & app.

konradkg
  • 671
  • 7
  • 11