表单数据 req.body 显示为未定义的缓冲区很好

问题描述 投票:0回答:0

我的服务器可以解析 JSON,但我是否需要一些额外的解析器来理解表单数据 req.body?我可以很好地读取 req.file.buffer 。还使用 multer 中间件。

const express = require('express')
const Controllers = require('./Controllers');
const app = express();
const port = 3001;
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const multer = require('multer');
const DIST_DIR = path.join(__dirname, '..', 'dist');
app.use('/', express.static(DIST_DIR));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const storage = multer.memoryStorage()
const upload = multer({ storage: storage })

app.use(cors({
  origin: 'http://localhost:3000'
}));

app.post('/links', upload.single("image"), (req, res) => {
  const imageName = req.file.filename;
  const description = req.body;
  const buffer = req.file.buffer.toString('base64');
  console.log(req.body);
  res.send();
});
node.js express multer
© www.soinside.com 2019 - 2024. All rights reserved.