Error Occurred While Trying to Proxy: localhost:XXXXapi with Nodejs, Express and Http-proxy-middleware

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.

New contributor

GiantEnemyShark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật