如果Express.js中使用了子路由器模块,我应该将中间件放在哪里
全部: 当我学习如何使用Express.js上传文件时,有一个称为multer()的中间件,从其使用示例可以看出:如果Express.js中使用了子路由器模块,我应该将中间件放在哪里,express,file-upload,Express,File Upload,全部: 当我学习如何使用Express.js上传文件时,有一个称为multer()的中间件,从其使用示例可以看出: var express = require('express') var multer = require('multer') var upload = multer({ dest: 'uploads/' }) var app = express() app.post('/profile', upload.single('avatar'), function (req, re
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express()
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
//
// e.g.
// req.files['avatar'][0] -> File
// req.files['gallery'] -> Array
//
// req.body will contain the text fields, if there were any
})
app.js中直接使用的中间件对象,我想知道是否使用子路由器,如:
app.js
var routes = require('./routes/index');
app.use("/", routes);
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/upload', function(req, res, next) {
debugger;
console.log(req.body.upld);
console.log(req.file);
res.send("");
});
module.exports = router;
index.js
var routes = require('./routes/index');
app.use("/", routes);
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/upload', function(req, res, next) {
debugger;
console.log(req.body.upld);
console.log(req.file);
res.send("");
});
module.exports = router;
我想知道我应该把那个上传中间件放在哪里?我应该在每个文件中使用var upload=multer({dest:'uploads/'})吗?如果是,这是否会导致根据路由器文件在不同文件夹中生成多个上载文件夹
谢谢