how to properly configure meteor to work with node file uploading module 'multer'

669 views Asked by At

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?

1

There are 1 answers

0
Archy Will He 何魏奇 On

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/