Recently, I started working with Express.js and I am using Postman to test my API. When running the code below, I successfully retrieve all members from the object:
// gets all members
app.get('/api/members', (req, res) => {
res.json(members)
})
For a visual representation of all members, you can check out this image link.
However, when attempting to access a single member from the API using the following code snippet, I encounter a 404 error in Postman:
// get single member
app.get('/api/member/:id', (req, res) => {
res.json(members.filter((member) => member.id === parseInt(req.params.id)))
})
You can view the specific error message received in Postman by visiting this link.
The provided code includes the full members object that has been transformed into an API:
const members = [
{
id: 1,
name: 'jack',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402a21232b002d256e232f2d">[email protected]</a>',
status: 'active'
},
{
id: 2,
name: 'sheldon',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4a7bcb1b8b0bbba94b9b1fab7bbb9">[email protected]</a>',
status: 'inactive'
},
{
id: 3,
name: 'matt',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04696570704469612a676b69">[email protected]</a>',
status: 'active'
},
]
module.exports = members
If anyone can shed light on what might be causing the error, it would be greatly appreciated...