Answer the question
In order to leave comments, you need to log in
Why does express return an empty req.body?
Please tell me, I raised the server locally / I
use express "^4.17.2"
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: false }))
app.use(express.json());
app.post('/api/text', (req, res) => {
console.log("---", req.body);
res.send("text");
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Answer the question
In order to leave comments, you need to log in
Express ^4x comes without body-parser, you need to include it separately
Option N1
Connect body-parser
npm install body-parser
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = 3000;
const bodyParser = require('body-parser');
app.use(express.urlencoded({ extended: false }))
app.use(express.json());
app.use(bodyParser.json())
app.post('/api/text',(req, res) => {
console.log("---", req.body);
res.send("text");
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
req.on('end', () => {
console.log("---", body);
});
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
const port = 3000;
app.use(express.urlencoded({ extended: false }))
app.use(express.json());
app.post('/api/text',(req, res) => {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log("---", body);
});
res.send("text");
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question