-3

I have two get request but in postman If I hit the second get request with the correct url then always run the first AND give response of the 1st request. optimize the question

router.get(
    "/:id",
    [auth],
    usersController.getUser
);

router.get(
    "/allUser",
     [auth],
    usersController.getAllUser
);

the userController file & relevant methods

exports.getUser = async (req, res) => {
  const id = req.params.id;
  try {
    let result = await userService.getUserAccount(id);
    if (result) {
      const resultWithoutPassword = {...result};
      delete resultWithoutPassword.password; 

      return res.status(HttpCodes.OK).send(resultWithoutPassword);
    }
    //console.error("error: Not Found.");
    return res.status(HttpCodes.NOT_FOUND).send(AppMessages.USER_NOT_FOUND);
  } catch (error) {

    return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(
      {
        Error: AppMessages.INTERNAL_SERVER_ERROR,
        Message: "Id Should be an Integer."
      });
  }
}


    try{
        let result = await userService.getAllUserAccounts();
        return res.status(httpCodes.OK).json(result);
    }
    catch(err){
      return 
           res.status(httpCodes.INTERNAL_SERVER_ERROR).json({ERROR: 
           AppMessages.INTERNAL_SERVER_ERROR});
    }

}
  • 1
    Because `/allUser` matches the pattern `/:id` ...? This will pass the value `allUser` to your `usersController.getUser` method now. You'd have to define those routes in opposite order. When you can have such "overlaps", then you always need to handle the more specific case first, and the less specific ones after. – CBroe Aug 16 '23 at 13:32
  • @CBroe Can you give an example or correct the given code? I didn't understand – Numaira Nawaz Aug 16 '23 at 13:36
  • 1
    What part of "do those two things in the opposite order" is unclear? https://stackoverflow.com/questions/32603818/order-of-router-precedence-in-express-js – CBroe Aug 16 '23 at 13:42
  • @CBroe when I put them in opposite order then I get always the response of the /allUser – Numaira Nawaz Aug 17 '23 at 06:13

1 Answers1

0

It sounds like you're experiencing unexpected behavior in Postman when making two different GET requests using Express.js routes. Specifically, when you send a request to the second route (/allUser), you're receiving a response from the first route (/:id) instead. This behavior is not aligned with what you expect.

Here are a few things you could check to troubleshoot and resolve the issue:

Route Order: Make sure the order of the routes in your Express router is correct. Routes are matched in the order they are defined. If the /:id route is defined before the /allUser route, it might be catching requests meant for the /allUser route. Swap the order of the routes if necessary.

Route Parameter Conflict: Ensure that the parameter defined in the /:id route is not conflicting with any possible values for the /allUser route. For example, if you're using a wildcard parameter like /:id, it might inadvertently match the string "allUser" and trigger the wrong route. You could modify the route parameter to make it more specific, if needed.

Route Middleware: Check if there's any middleware applied to the routes that might be causing unexpected behavior. In this case, the [auth] middleware could be influencing how the routes are processed. Review the middleware to ensure it's not interfering with the routes' normal behavior.

Route Path Overlap: Double-check that the route paths are distinct and don't overlap. For example, if the route paths are /allUser and /:id, there shouldn't be any ambiguity in how Express matches incoming requests to these routes.

Here's how you might adjust your route definitions for clarity:

// Define the routes in the correct order
router.get(
    "/allUser",
    usersController.getAllUser
);

router.get(
    "/:id",
    [auth],
    usersController.getUser
);

Remember that without seeing the complete code and context, it's challenging to pinpoint the exact issue. However, these general troubleshooting steps should help you identify and address the problem.

Aziz Saidi
  • 11
  • 3