How to debug Node.js applications: a step-by-step guide for beginners
Hi everyone! I just started learning Node.js and am writing my first server on Express. I ran into a problem: my application is not working as I expected. Here's the simplified code:
```javascript
const express = require('express');
const app = express();
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
const user = getUserById(userId);
res.json({ user: user });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
function getUserById(id) {
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
return users.find(u => u.id === id);
}
```
I'm making a GET request to `/users/1`, but I always get `{ "user": undefined }`. I don't understand why `find()` is not finding the user. I tried inserting `consol