I am new to exploring express.js and attempting to create a basic sign-in example server. I have set up a database object with a users array containing objects, each with email and password properties. Despite using body-parser to convert the body into a javascript object, I keep encountering an error in response. Below is the code snippet:
const app = express();
app.use(bodyParser.json())
const database = {
users: [
{
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afc5cecc...">[email protected]</a>',
password: 'somepw'
},
{
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="21425250...">[email protected]</a>',
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 sent through postman:
{
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="056f64666...">[email protected]</a>",
"password": "somepw"
}
Despite the request matching the database data, it still returns an error. Hopefully, the issue has been explained clearly. Thank you.