将Multer
与express.js
一起使用时出现问题。所以这很好用:
const multer = require('multer');
let upload = multer({dest: 'uploads/'});
app.post('/upload', upload.single('image'), function (req, res, next) {
console.log(req.file)
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
});
但是当我尝试这样做时,req.file is undefined
:
const multer = require('multer');
let upload = multer({dest: 'uploads/'});
router.route('/', upload.single('image'))
.post(function (req, res, next) {
console.log(req.file);
let memory = new Memory();
Object.assign(memory, req.body);
memory.save(function (err) {
if (err) {
return res.send(err);
}
res.json({message: 'Memory Created', memory});
});
})
只是想知道如果我想从我的routes.js
分隔server.js
,那么正确的语法是什么。
我也试过这个:
const multer = require('multer');
let upload = multer({dest: 'uploads/'});
router.route('/')
.post(upload.single('image'), function (req, res, next) {
console.log(req.file);
let memory = new Memory();
Object.assign(memory, req.body);
memory.save(function (err) {
if (err) {
return res.send(err);
}
res.json({message: 'Memory Created', memory});
});
})
由于