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

有些话、适合烂在心里 提交于 2019-12-05 02:35:12

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