我是使用multer在node-express应用中上传文件的新手。我尝试在路由中传递中间件(从单独的文件导入),但在控制台中收到错误消息:
router.post('/', uploadMW.multerFunction('/articles').any('image'), articleController.article_create);
^
TypeError: uploadMW.multerFunction is not a function
at Object.<anonymous> (F:\wordify-web\backend-wordify\api\routes\articles.js:9:27)
这是我传递中间件的路由器文件代码:
const express = require('express');
const router = express.Router();
const articleController = require('../controllers/articles');
const uploadMW = require('../middleware/multer-file');
router.get('/', articleController.article_get_all);
router.post('/', uploadMW.multerFunction('/articles').any('image'), articleController.article_create);
router.get('/:articleId', articleController.article_by_id);
module.exports = router;
这是multer文件代码:
const express = require('express');
const router = express();
const multer = require('multer');
let storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.filename + '-' + Date.now() + path.extname(file.originalname))
}
})
const upload = multer({storage: storage}).single("image");
module.exports = upload;
请帮助解决。