Salah Eddine Makdour
Salah Eddine Makdour

Reputation: 1042

why is the express server responding with error eventhough everything is correct

I'm new to using express.js and i'm trying to make a simple sign in example server so i made a database object that has a users array of object and each object if the users array has the properties if email and password, the problem is that eventhough i used body-parser to parse the body into a js object, it always responds with error, here is the code

const app = express();
app.use(bodyParser.json())

const database = {
    users: [
        {
            email: '[email protected]',
            password: 'somepw'
        },
        {
            email: '[email protected]',
            password: 'dkjfzqsdiof'
        }
    ]
}


app.post('/signin', (req, res) => {
    if (req.body.email === database.users[0].email && req.body.password === database.users[0].password) {
        res.json('success')
    } else {
        res.json('error')
    }
})

app.listen(3000, () => {
    console.log('app is listening')
})

the request i've sent with postman:

{
    "email": "[email protected]",
    "password": "somepw"
}

as you can see the request matches the data in the database and it still responds with error. Hope i could describe the problem well, thanks

Upvotes: 0

Views: 24

Answers (1)

mickl
mickl

Reputation: 49945

The missing part was a content-type header which is required when POSTing JSON (link). It can be easily turned on in Postman:

enter image description here

Upvotes: 1

Related Questions