路由中的节点multer

时间:2015-05-20 17:41:13

标签: node.js

我在我的app.js中更喜欢:

var multer  = require('multer');
app.use(multer({ dest: './public/img/profile/',
 rename: function (fieldname, filename) {
    return fieldname;
  },
onFileUploadStart: function (file) {
  console.log(file.originalname + ' is starting ...')
},
limits: {
  files: 1
},
onFileUploadComplete: function (file) {
  console.log(file.fieldname + ' uploaded to  ' + file.path)
  imageUploaded=true;
}
}));

这可以,但我想在一个特定的路线中设置这些设置。例如imageRoute.js而不是我的app.js.但是我应该使用什么代替app.use()而不是因为我无法访问我的路线中的应用程序。我在我的app.js中创建路线,如:

  var imageRoutes  = require('./routes/imageRoutes')();
  app.use('/image', imageRoutes);

imageRoutes.js

module.exports = function(passport) {

var that = this;

// Modules
var express = require('express');
var router = express.Router();
var fs = require('fs');

// Middleware: Checks if user is authenticated removed for more readable code


router.post('/fileupload', that.ensureAuthenticated, function(req, res, next) {
   //setup multer here 
});

 router.post('/fileupload2', that.ensureAuthenticated, function(req, res, next) {
   //setup multer here with a different destination path/filename etc
});




return router;
}

我发现this question但它并没有真正帮助我。

更新

我现在用以下内容编辑了我的app.js

 var multer  = require('multer');
 var imageRoutes= require('./routes/imageRoutes')(someRepo, multer);

imageRoutes.js

// POST: CREATE avatar
router.post('/avatar', function(req, res) {
    router.use(multer({
        dest: './public/img/profile/',
        rename: function (fieldname, filename) {
            return fieldname;
        },
        onFileUploadStart: function (file) {
            console.log(file.originalname + ' is starting ...')
        },
        limits: {
            files: 1
        },
        onFileUploadComplete: function (file) {
            console.log(file.fieldname + ' uploaded to  ' + file.path)
            imageUploaded=true;
            console.log(req.files);
            res.redirect('/');
        }
    }))

}); 

我看到chrome正在将文件上传到100%但是它什么也没做。它没有开始..或任何其他错误。

1 个答案:

答案 0 :(得分:2)

您可以将多个中间件/路由器传递给.use()

var multer  = require('multer');
var parseUploads = multer({
  dest: './public/img/profile/',
  rename: function (fieldname, filename) {
    return fieldname;
  },
  onFileUploadStart: function (file) {
    console.log(file.originalname + ' is starting ...')
  },
  limits: {
    files: 1
  },
  onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
    imageUploaded=true;
  }
});

// ...

var imageRoutes  = require('./routes/imageRoutes')();
app.use('/image', parseUploads, imageRoutes);

如果您想将逻辑完全移至imageRoutes.js且您的multer逻辑路由特定,您可以执行以下操作:

router.post('/avatar', multer({
  dest: './public/img/profile/',
  rename: function (fieldname, filename) {
    return fieldname;
  },
  onFileUploadStart: function (file) {
    console.log(file.originalname + ' is starting ...')
  },
  limits: {
    files: 1
  },
  onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path)
  }
}), function(req, res) {
  // Here you can check `Object.keys(req.files).length`
  // or for specific fields like `req.files.imageField`
  res.redirect('/');
});