Search code examples
javascriptnode.jsexpresspostman

Some requests work and others don’t on postman


I'm a very new geek trying to build an application using NodeJs and ExpressJs. I'm testing the backend with postman. some of the requests work perfectly but others don’t, I've been getting cannot get the url message, 404 not found for hours now, and as I mentioned other routes work very good.

Everything seems ok Server side, type content is JSON, I've checked the code multiple times and I can’t see the error. any ideas please? Thank you 🙏.

The server: the server Postman response: Postman response

The model:

module.exports = (_db) => {
  db = _db;
  return SearchModel;
};
class SearchModel {
  //Get a product by name
  static getProductByName(productName) {
    return db
      .query('SELECT * FROM Products WHERE productName = ?', [
        productName
      ])
      .then((res) => {
        console.log(res);
        return res;
      })
      .catch((error) => {
        console.log(error);
        return error;
      });
  }
  // Get products by gender
  static getProductByGender(gender) {
    return db
      .query('SELECT * FROM Products WHERE gender = ?', [gender])
      .then((res) => {
        return res;
      })
      .catch((error) => {
        return error;
      });
  }
  // Get products by brand
  static getProductByBrand(brand) {
    return db
      .query('SELECT * FROM Products WHERE brand=?', [brand])
      .then((res) => {
        return res;
      })
      .catch((error) => {
        return error;
      });
  }
  // Get products by mouvement
  static getProductByMouvement(mouvement) {
    return db
      .query('SELECT * FROM Products WHERE mouvement=?', [mouvement])
      .then((res) => {
        return res;
      })
      .catch((error) => {
        return error;
      });
  }
}

The route:

  // Get a product by name
  module.exports = (app, db) => {
  const searchModel = require('../models/SearchModel')(db);

  // Route pour afficher un produit par son nom
  app.get('/api/v1/Search/name', async (req, res) => {
    const productByName = await searchModel.getProductByName(
      req.body.productName,
    );

    if (productByName.code) {
      res.json({ status: 500, msg: 'Server Error!' });
    } else if (!productByName) {
      res.json({ status: 404, msg: 'Product not found!' });
    } else {
      res.json({ status: 200, result: productByName, msg: 'Voilà!' });
    }
  });
};

Solution

  • the problem for me was that I forgot to add the searchRoute to the db in the server.js! 2days for that 😑, but everything working well now.

    app.get('/', async (req, res) => {
              res.json({ status: 200  });
            });
            userRoute(app, db);
            productRoute(app, db);
            orderRoute(app, db);
            authRoute(app, db);
            searchRoute(app, db);
          })
          .catch((error) => console.log(error));
    

    Thanks for your answers, They made me do some research and learn a few things