如何在流星应用程序中正确配置节点中间件

时间:2015-06-09 18:53:11

标签: node.js meteor

我已安装了multer中间件,可用于我的meteor应用程序:

{
        "multer": "0.1.8"
}

我有meteorhack:npm也安装了

现在,我正在尝试以这种方式配置middlware:

var multer = Meteor.npmRequire('multer');

Meteor.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;
    }
}));

当只有第一行存在时,我没有任何问题。

但是,当我尝试实际配置中间件时,我收到以下错误:

W20150609-13:51:29.641(-5)? (STDERR)          
W20150609-13:51:29.641(-5)? (STDERR) /home/eugene/.meteor/packages/meteor-tool/.1.1.3.4sddkj++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20150609-13:51:29.641(-5)? (STDERR)                        throw(ex);
W20150609-13:51:29.641(-5)? (STDERR)                              ^
W20150609-13:51:29.644(-5)? (STDERR) TypeError: Cannot call method 'use' of undefined
W20150609-13:51:29.644(-5)? (STDERR)     at app/server/startup/loadMulter.js:4:6
W20150609-13:51:29.644(-5)? (STDERR)     at app/server/startup/loadMulter.js:18:3
W20150609-13:51:29.644(-5)? (STDERR)     at /home/eugene/dev/meteor/socially/.meteor/local/build/programs/server/boot.js:222:10
W20150609-13:51:29.644(-5)? (STDERR)     at Array.forEach (native)
W20150609-13:51:29.644(-5)? (STDERR)     at Function._.each._.forEach (/home/eugene/.meteor/packages/meteor-tool/.1.1.3.4sddkj++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150609-13:51:29.644(-5)? (STDERR)     at /home/eugene/dev/meteor/socially/.meteor/local/build/programs/server/boot.js:117:5

看来,Meteor对象没有名为“app”的属性。

在这种情况下,完成此任务的正确语法是什么?

1 个答案:

答案 0 :(得分:1)

Meteor使用connect来处理服务器端路由和中间件。

webapp包公开了一个WebApp对象,该对象具有connectHandlers属性,您可以在其上添加自己的中间件。

WebApp.connectHandlers.use(multer({
  dest: './uploads/',
  [...]
}));

https://docs.meteor.com/#/full/webapp