Multer与Express.js

时间:2017-12-13 15:38:14

标签: javascript node.js multer

Multerexpress.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});
        });
    })

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

carbo2 <- carbo[,-4,-5,]
#or
carbo2 <- carbo[,c(-4,-5),]

确保帖子请求正文将文件附加到router.post('/', upload.single('image'), function (req, res, next) { console.log(req.file); // Do something with the file }); 属性。如果您使用Postman进行测试,它应该是这样的:

enter image description here