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

╄→гoц情女王★ 提交于 2020-01-13 08:11:27

问题


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:


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/


来源:https://stackoverflow.com/questions/30714445/how-to-properly-configure-meteor-to-work-with-node-file-uploading-module-multer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!