I have been trying to pass data from my React component to the node server.js. In my React component, inside the componentWillMount, I am making an axios post call and pass countValue.
Here is my code for the React Component:
componentWillMount = () => {
axios.post("/", {
countValue: 12
});
}
Inside my server.js, I am simply trying to get countValue through req.body.countValue. However, it is always set to "undefined".
req.body just comes out to be empty object, {}.
Here is my code for the server.js
const express = require('express');
const bodyParser = require('body-parser');
const engines = require('consolidate');
const app = express();
app.engine("ejs", engines.ejs);
app.set('views', __dirname);
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
console.log(req.body.countValue);
res.render("index");
});
Could anyone please help me with this issue?
app.use(express.json())orapp.use(bodyparser.json())(needsbody-parserpackage)axios.post("/", {probably require full uri including scheme (like http or https etc) like:axios.post("http://localhost:port" ...)