I'm just starting with meteor.
I've found, and added the 'multer' package:
meteor add pfafman:multer
Now, I wonder how to configure the server side of meteor to use.
In my plain node app, I use it like this:
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path);
var fileName = file.name;
var done=true;
}
}));
What is the equivalent server code for this in Meteor?
pfafman's multer is nothing more than a wrapper around npm's multer (if you go through its source you'd realize all it really does is
multer = Npm.require('multer');
and then export it as a global variable through api.export('multer');
(He didn't even include any test cases, let along a demo.))
In express, app.use()
is used for adding middleware 'layers' to the middleware stack. Since now multer
is already a global variable accessible anywhere on the server (after you have meteor add pfafman:multer
), you can just use it the way you do in express by calling it in Meteor.startup
:
if (Meteor.isServer) {
Meteor.startup(function () {
multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path);
var fileName = file.name;
var done=true;
}
})
});
}
Note: this would create the uploads
directory in
YourMeteorProject/.meteor/local/build/programs/server/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With