I have two GET requests, but in Postman, if I hit the second GET request with the correct URL, the first request is always run, and the response from the first request is given. Optimization problem
router.get( "/:id", [auth], usersController.getUser ); router.get( "/allUser", [auth], usersController.getAllUser );
User controller files and related 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}); } }
It sounds like you are experiencing unexpected behavior when making two different GET requests in Postman when using Express.js routing. Specifically, when you send a request to the second route (/allUser), you receive a response from the first route (/:id). This behavior is inconsistent with your expectations.
Here are some things you can check to resolve the issue:
Routing order: Make sure the routing order in the Express router is correct. Routes are matched in the order in which they are defined. If the /:id route is defined before the /allUser route, it may catch requests that should be sent to the /allUser route. The order of routes can be swapped if desired.
Route parameter conflicts: Ensure that parameters defined in the /:id route do not conflict with any possible values of the /allUser route. For example, if you use a wildcard parameter such as /:id, it may accidentally match the string "allUser" and trigger the wrong route. If needed, the route parameters can be modified to make them more specific.
Routing middleware: Check whether any middleware applied to the route may cause unexpected behavior. In this case, the [auth] middleware may affect how routing is handled. Please check the middleware to make sure it does not interfere with the normal behavior of routing.
Routing paths overlap: Double check whether the routing paths are different and non-overlapping. For example, if the route paths are /allUser and /:id, Express should not have any ambiguity in matching incoming requests to these routes.
Here's how you can adjust your route definitions to improve clarity:
Keep in mind that it is difficult to pinpoint the problem without seeing the complete code and context. However, these general troubleshooting steps should help you identify and resolve the problem.