node.js - 如何在 meteor 应用程序中正确配置 Node 中间件

标签 node.js meteor

我已经安装了 multer 中间件以在我的 Meteor 应用程序中使用:

{
        "multer": "0.1.8"
}

我还安装了meteorhack:npm

现在,我尝试以这种方式配置中间件:

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”的属性。

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

最佳答案

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

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

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

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

关于node.js - 如何在 meteor 应用程序中正确配置 Node 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740463/

相关文章:

javascript - 如何通过将模式引用推送到用户的数组字段来更新用户对象

ssl - 没有 ssl 证书的暂存系统上的 Meteor force-ssl?

node.js - 将 Node 模块与 Meteor 一起使用

javascript - React Router 总是说没有路由,即使它们已经设置好了

javascript - 在vue js中动态导入文件

javascript - 同构 JS - 仅限 httpRequest 客户端

javascript - Jest - 未导出的模拟函数

node.js - mongoose findByIdAndUpdate 仅更新文档中的一个字段

javascript - MongoDB 中插入失败

mysql - 在环回中使用远程方法增加模型中的字段?