我尝试将表单数据发送到nodejs,但req.body始终为空,并且req.files未定义。这是我的代码 的 index.js
const express = require('express');
const app = express();
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const config = require('./config/database');
const path = require('path');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
const router = express.Router();
const products = require('./routes/product')(router);
const port = process.env.PORT || 3030;
app.use('/products', products);
app.listen(port, () => {
console.log('Listening on port ' + port + ' in ' + process.env.NODE_ENV + ' mode');
});
并在product.js文件中
let multer = require('multer');
let upload = multer();
module.exports = (router) => {
router.post('/newProduct', (req, res) => {
console.log('req');
console.log( req.body );
console.log( req.files );
})
}
答案 0 :(得分:0)
我建议至少阅读multer readme
您需要告诉multer上传每条路线的文件。
例如
WHERE
或者您也可以将INNER JOIN
用于单个文件,或router.post('/newProduct', upload.array('files', 5), (req, res) => {
console.log('req');
console.log( req.body );
console.log( req.files );
})
来处理多个上传表单字段。但所有这些都在他们的页面中得到了更好的解释。