title: Nodejs restful API: how to get data by id
body: <p>I am building a node/express app and I want to return a specific data by handling id params.</p>
  
  <p>Here is the structure of my files:</p>
  
  <pre><code>    app.js
      ./src
        /db/users.js
        controllers
        models
      ./tests
  
  </code></pre>
  
  <p>users.js</p>
  
  <pre><code>const users = [
    {
      id: 1,
      email: 'sarah@gmail.com',
      name: 'Sarah',
      phone: 09000082
  
    },
    {
      id: 2,
      email: 'dklf@gmail.com',
      name: 'Kelavos',
      phone: 08522222
  
    },
  ];
  
  export default users;
  </code></pre>
  
  <p>My model</p>
  
  <pre><code>import users from '../db/users';
  
  const findUser = id =&gt; users.find(x =&gt; x.id === id);
  
  export default { findUser };
  </code></pre>
  
  <p>My controller</p>
  
  <pre><code>import userModel from '../models/user.model';
  
  class UserController {
  
    /**
   * Get one user
   * @param {object} req
   * @param {object} res
   */
  
    getUser(req, res) {
      const id = parseInt(req.params.id);
      console.log(`Test: ${  userModel.findUser(req.params.id)  } and ${  req.params.id}`);
      return res.status(201).send({
        status: 'success',
        data: userModel.findUser(id),
      });
  // return Not found and 404 for an non existing id or if id !== int
      if (isNaN(id) || !id) {
        return res.status(404).send({
          status: 'error',
          error: 'Not found',
        });
      }
    }
  }
  
  const userController = new UserController();
  export default userController;
  </code></pre>
  
  <p>This is my <code>app.js</code></p>
  
  <pre><code>import express from 'express';
  import bodyParser from 'body-parser';
  import UserController from './src/controllers/user.controller';
  
  // set up the express app
  const app = express();
  
  // Parse incoming requests data
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  
  const baseUrl = '/api/v1/';
  
  app.get(`${baseUrl}users/:id`, UserController.getUser);
  
  export default app;
  </code></pre>
  
  <p>I've set the port and other config in a server.js file</p>
  
  <p>If I run the app and test the endpoint with Postman I only get <code>status: "success"</code>
  Not that I comment the second bloc of UserController class <code>if (isNaN(id) || !id)</code> because with it ESLint warn me of <code>Unreachable code detected</code></p>
  
  <p>What is wrong with my code that is preventing me to return the result for a given user?</p>