I am using multer (^0.1.6) in a Nodejs Kraken application to upload a file. I looked at the multer documentation on npm and it seemed it's pretty straightforward to use this module for file upload.
But I have been facing issues while uploading file on my local machine. Interestingly, I notice the file gets uploaded but my server hangs and the control never goes to the next middleware in the application which is responsible for rendering the next page.
I also noticed that when the server is hung, it tried to upload the file again and finally times out saying "No data received". As a result of multer's retry, I see two copies of same file uploaded.
Here is my kraken application code using multer module:
index.js (root leve index.js at same level as package.json)
-------------------------------------------------------------
...
.....
app = module.exports = express();
app.use(kraken(options));
app = module.exports = express();
app.use(kraken(options));
app.on('start', function () {
console.log('Application ready to serve requests.');
console.log('Environment: %s', app.kraken.get('env:env'));
});
app.use(multer({
dest: '/Users/abc/temp/fileuploads',
limits: {
fieldNameSize: 500,
files: 2,
fields: 5
},
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());
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);
}
}));
index.js (under controllers directory of Kraken application)
-------------------------------------------------------------
console.log("File Uploaded");
model.status = "File Uploaded!!";
var body = req.body;
console.log("File attributes: " + JSON.stringify(body));
var files = req.files;
console.log("Files: " + JSON.stringify(files));
res.render('uploadfile/datauploadform', model);
});
Here are the application logs after I start uploading the file (notice multer tried to upload same file twice before timing out):
Form parsing started at: Sat Jan 10 2015 01:29:45 GMT-0800 (PST)
Upload starting for filename: HelloNashorn.js
35 of file arrived
file uploaded to /Users/abc/temp/fileuploads/fileHelloNashorn1420882185450.js
Form parsing completed at: Sat Jan 10 2015 01:29:45 GMT-0800 (PST)
Form parsing started at: Sat Jan 10 2015 01:31:45 GMT-0800 (PST)
Upload starting for filename: HelloNashorn.js
35 of file arrived
file uploaded to /Users/abc/temp/fileuploads/fileHelloNashorn1420882305450.js
Form parsing completed at: Sat Jan 10 2015 01:31:45 GMT-0800 (PST)
Appreciate any help!