Home > Back-end >  express req.body is returing an empty json object
express req.body is returing an empty json object

Time:05-01

I have read all the other posts and I have verified none of the solutions on any of them solve my problem. In insomnia I am setting the content type header to "application/json" and I am applying the express.json() middle ware before any of my routes. The MOST frustrating thing is the body isnt empty in my /register route however it always is inside my /login route. I have even tried putting the middle ware directly into the route with "router.get('/', express.json(), async (req, res) => {" but it was to no avail. Also when I make the route a post I get a "cannot GET /register" with code 404 but not when its a "get" route. On my register route there is both a get and post route so I cant fathom why its any different with the login route ESPECIALLY since I made the register route by copy and pasting the login routes js file. Here is the code for the app

const express = require("express"),
  bodyParser = require('body-parser'),
  rta = require('./RTAuth.js'),
  mysql = require('mysql'),
  app = express(),
  port = 8080;

//app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//app.use(bodyparser.urlencoded({ extended : true }));
//app.use(express.json({ limit: "200mb" }));

app.use(function(req,res, next) {
  res.locals.loginDB = mysql.createConnection({
    host: "localhost",
    user: "removed before posting",
    database: "removed before posting",
    password: "removed before posting"
  });
  res.locals.rta = rta;
  next();
});


//ImportRoutes
const loginRouter = require("./routes/login");
const registerRouter = require("./routes/register");

//Setup ImportRoutes
app.use("/AuthTest/login", loginRouter);
app.use("/AuthTest/register", registerRouter);


app.listen(port, () => {console.log("its alive!")});

I included commented out code I have tried. Now here is the problematic route up until the part it fails so ignore missing brackets after the return, the rest is irrelevant as its flow is cut off by the return statement.

const express = require("express");

//Setup express server routers
const router = express.Router();

//On get request
router.get('/', async (req, res) => {
  var test = req.body;
  res.status(200).send({
    success: false,
    message: "Test.",
    body: test,
    headers: req.headers
  });
  return;

Image of post result after making the route a post route

CodePudding user response:

OMG THANK YOU @Phil, what he said about the 404 code meaning the post was still trying to do a get request made me suddenly remember a long while ago I made a folder named "login" in the apps folder in my websites publichtml folder. Meaning instead of calling express it was interpreting my request as a get, I believe because after removing the folder I was able to use the endpoint /login instead of /logins which worked and was the nail on the head to solving this!

  • Related