skip to Main Content

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à!' });
    }
  });
};

2

Answers


  1. Are you sure that in GET request params are in body?

    This is the correct way:

    app.get('/path', (req, res) => {
      const productName= req.query.productName;
    
      // Do something with the query parameters
      console.log(productName);
      
      res.send('Query parameters received');
    });
    

    And since the product is not found, you return 404 which means page not found as far as PostMan shows.

    Login or Signup to reply.
  2. req.body in GET and DELETE request is not valid, you should use params or queries instead. Like:

    app.get('/api/v1/Search/:name', async (req, res) => {
      const name = req.params.name
      console.log(name) //    try /api/v1/Search/abc => name = 'abc'
    })
    // or
    app.get('/api/v1/Search', async (req, res) => {
      const name = req.query.name
      console.log(name) //    try /api/v1/Search/?name=abc => name = 'abc'
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search