Receiving 404 error on Post Request using Postman MERN Stack

Viewed 794

I am Trying to add login functionality for my Model(User). I have checked my database connection it is working well because other routes are working fine. Like when I create a user it creates a user successfully. So I m sure that there is no certain problem with my database or my database connection. even handfull of my code is working fine.

I m sending the POST request with the following information

{
        "email":"sherryijan@gmail.com",
        "password":"6421515"
}

enter image description here

app.post("api/users/login", (req, res)=>{
    //find email
    User.findOne({email: req.body.email}, (err, user) =>{
        if(!user) return res.json({
            loginSuccess: false,
            message: "Auth Failed, email not found"
        });
        //compare password

        user.comparePassword(req.body.password, (err, isMatch) => {
            if(!isMatch){
                return res.json({loginSuccess:false, message:"wrong password"})
            }
        })
        //generate token
        user.generateToken((err, user)=>{
             if(err) return res.status(400).send(err);
             res.cookie("x_auth", user.token)
                .status(200)
                .json({
                    loginSuccess: true
                })
        })
    })
})

the error it shows is

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot POST /api/users/login</pre>
</body>

</html>

I m using postman.

1 Answers

The issue that you have is here: app.post("api/users/login", (req, res)=>{. Your path does not start with a / which means that your express app doesn't expose that path at all for your clients (postman) to use.

How can you tell what's going on:

The response code is 404 which means right away that the resource does not exist. So the first thing you look at is the paths. The one in postman doesn't match the one in express.

Related