Alright so I’m guessing this should have a simple solution, but I can’t seem to figure it out. I made the frontend first and now I’m want to make the backend so I can connect to a database.
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function (req, res) {
res.send("hello");
});
app.post("/", function (req, res) {
console.log(req.body);
});
app.listen(3001, function () {
console.log("listening on 3001");
});
And this is the post request I’m making on my react frontend.
axios.post("http://localhost:3001/", JSON.stringify(note));
note is an object like such
{title: "",content: ""}
the empty string gets filled out with submission data.
when I make the post request this is what gets logged in the console
{ '{"title":"test","content":"one"}': '' }
I had to use JSON.stringify() to show whats being passed through but without it, my output is {}
So the problem is when my object is posted it becomes the key of an object with empty values
What I want to do is simply send the whole object like so
axios.post("http://localhost:3001/", note);
so that in the backend I can tap into the values by doing req.body.title
and req.body.content