I’m starting to use http-proxy-middleware
and other tools, like Express
, NodeJS
, etc. to make a Gateway API for a microservice I’m working on. Specifically, I’m using this guide here. I think I understand well how the setup work, specifically the part on “routes” and “target”.
However, when I try to test the connection with one of my services on Postman, I get this odd error that says "Error occurred while trying to proxy: localhost:3000"
, with the “/” after the port number not being showed and the error code being 504, or "Gateway timeout"
. Also, the terminal that I used to run the Gateway using Visual Studio code returns this message for the request, but I’m not sure if it means anything specific:
::1 - - [Date +0000] "GET / HTTP/1.1" 504 - "-" "PostmanRuntime/7.39.0"
.
So, for example, if I use localhost:3000
for my gateway, if I try to go to "localhost:3000/getStuff"
to show certain elements from a list, I feel like the Gateway should call "localhost:3001/getStuff"
(which is the port my other service is on) to get the elements from said list. At least that’s how I understand how http-proxy-middleware
works. However, instead, I get the error "Error occurred while trying to proxy: localhost:3000getStuff"
.
I’ve looked for a solution online for quite a bit, but it never seems to be my exact error. For example, I’ve seen questions that mentions a similar text, but would include extra text or information, like "Error occurred while trying to proxy request /login from localhost:4200 to <TARGET>"
(such as here or here). Either that, or the people asking those questions would use different tools than me, which I feel would not be all that helpful, or the questions don’t have any answers.
Recreating the error should be pretty simple. Just make a project that consist of two Nodejs/Javascript projects/services in seperate sub-folders. After that, follow the step in the first link I showed to recreate the gateway using one of the sub-folders, while also making sure to correctly set the appropriate “routes” and “target” values. Then, for the other service, just create a simple express request in a “index.js” file, while also making sure that all dependencies and scripts are correct so that it runs correctly, like so:
app.get('/', async (req, res) => {
res.status(200).send("Request sent through gateway successful.");
});
Finally, after running both services on different ports, either go to the localhost for the API Gateway service (like “localhost:4000”), or doing a test request using a tool like Postman. After a few seconds, it should give you back the same error I had.
Lastly, it’s also just possible that my problem comes more from me being more of a beginner in trying to make a custom API Gateway for my project, so if the tutorial I used is not good, I would like to be let know about it, and maybe get a better guide to set up a better custom Gateway for a microservice project.
My Nodejs version is 20.13.1. Everything else should be the default/up-to-date, such as with express and http-proxy-middleware.
Thank you in advance.
GiantEnemyShark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.