'I am getting error "POST http://localhost:3001/auth/login 400 (Bad Request)"
I am new to node js and am developing a simple website. when sending an unregistered username to the server, instead of getting the "username/password does not exist", I am getting "POST http://localhost:3001/auth/login 400 (Bad Request)". can someone plz help me figure out this problem? thank you.
my middleware
my Post request
my route
const router = require("express").Router();
const {loginUser, registerUser} =
require("../controllers/authController.js");
router.post('/register', registerUser);
router.post('/login', loginUser);
module.exports = router;
Solution 1:[1]
Found the error. I was using my route middleware before using the cookie and it was somehow throwing this error. sorry, for not providing the complete code, and thanks for all the reply.
Solution 2:[2]
I think you are getting this error because you are not passing the userdata to the login function. I'm not sure but try using below code:
const login = (loginUser, loginPassword) => {
axios.post('http://localhost:3001/auth/login', {
username: loginUser,
password: loginPassword
}).then(...)
}
Solution 3:[3]
You have a route issue! in you route you have:
router.post('/login', loginUser);
And in you test you have:
http://localhost:3000/auth/login
So you need to either add auth in your route definition or remove auth in your axios post
Solution 4:[4]
Many things can go wrong. I recommend you to just run the app with a debugger and just check line by line where something is wrong. I see you are using Visual Studio Code. There are many Youtube videos on how to run the app with the debugger.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | totalNoob |
| Solution 2 | Dharmik Patel |
| Solution 3 | Alaindeseine |
| Solution 4 | Krystian Sztadhaus |


