0

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

  • GET requests typically don't have a request body. If the request comes from a browser, it's literally impossible – Phil Apr 29 '22 at 05:12
  • @Phil I wasnt aware of that, but when I change it to be a post request it says "Cannot GET /AuthTest/login/" with a 404 – Dabbo Pabblo Apr 29 '22 at 05:19
  • If you make it a POST handler and get a 404 error, then something is still trying to make a GET request and you'd need to fix that in the client code. Also if you have any redirect responses to `/login`, be aware that it will also result in a GET request – Phil Apr 29 '22 at 05:19
  • @Phil I dont have any client code I said I am using Insomnia to make requests, I literally can only be sending the single request I am trying to send. What do you mean by redirect responses? I have my route js files in a separate /routes folder and I use const loginRouter = require("./routes/login"); app.use("/AuthTest/login", loginRouter); – Dabbo Pabblo Apr 29 '22 at 05:24
  • Do you use `res.redirect()` anywhere. Are you making a POST request in Insomnia? – Phil Apr 29 '22 at 05:25
  • @Phil No I don't use `res.redirect()` anywhere and yes I am making a post request in insomnia – Dabbo Pabblo Apr 29 '22 at 05:28
  • @Phil I edited it to include the screenshot, thanks for taking the time to help help me – Dabbo Pabblo Apr 29 '22 at 05:32
  • Did you change the login route handler to `router.post('/', (req, res) => { ... })`? – Phil Apr 29 '22 at 05:33
  • @Phil yes I did change the route handler to that – Dabbo Pabblo Apr 29 '22 at 05:33
  • I think I see the problem. Your original request is to `/AuthTest/login` but the error says `/AuthTest/login/` (trailing `/`). Something is trying to redirect the request via a 301 response. Do you have a reverse proxy configured? If so, it seems to be misconfigured – Phil Apr 29 '22 at 05:42
  • @Phil ok I changed `app.use("/AuthTest/login", loginRouter);` to `app.use("/AuthTest/logins", loginRouter);` I literally just added an s to login and now not only is the post working its also returning a req.body, do you have any clue why this solved it and /login doesn't work? – Dabbo Pabblo Apr 29 '22 at 05:44
  • @Phil you were 100% correct! Except the redirect wasnt happening on the app it was happening by my website because I had a folder named login in my apps folder inside publichtml, so my website assumed I was trying to get the index.html file inside there even tho the folder was empty and I thought it was needed before I had implemented the working register route. Thank you for the help I actually probably wouldn't of put the 2 and 2 together if you hadn't given me that insight into what the 404 meant! – Dabbo Pabblo Apr 29 '22 at 05:52

1 Answers1

1

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!