The purpose of this repo is to demonstrate a problem with a NestJS middlware combined with the npm package http-proxy-middlware
. There's a corresponding StackOverflow post referring to this repo.
- Clone the repo
- Run
npm install
- Fire up the Todos API via
ng serve todos-api
. Runcurl http://localhost:8090/api
and you should receive a 200 OK response with an array of todos. - Fire up the API Gateway via
ng serve api-gateway
. Runcurl http://localhost:8080/api
and you should receive a 200 OK response with a JSON response{"ok":true}
.
The following requests are sent to the API Gateway on port 8080
which serves as kind of a reverse proxy for the Todos API which runs on port 8090
.
- Requests made to
http://localhost:8080/api/v1/...
are reverse-proxied through a global functional middleware which is setup inmain.ts
. - Requests made to
http://localhost:8080/api/v2/...
are reverse-proxied through a NestJS Middlware which is setup inreverse-proxy.middleware.ts
and registered inapp.module.ts
.
In the following requests you'll see that the GET requests are proxied successfully through both kinds of middlwares. The PUT request, though, is only proxied successfully throug the global functional middlware. Proxying through the Nest JS Middeware fails, the client never receives a response (it's kinda "hanging").
- Run
curl http://localhost:8080/api/v1/todos-api
and you should receive the array of todos. - Run
curl http://localhost:8080/api/v2/todos-api
and you should also receive the array of todos. - Run
curl -X PUT -H "Content-Type: application/json" -d "{\"id\": 1, \"userId\": 1, \"title\": \"delectus aut autem - v1\", \"completed\": true}" http://localhost:8080/api/v1/todos-api/1
and you should receive a 200 OK and the todo should have been updated successfully. - Run
curl -X PUT -H "Content-Type: application/json" -d "{\"id\": 1, \"userId\": 1, \"title\": \"delectus aut autem - v2\", \"completed\": true}" http://localhost:8080/api/v2/todos-api/1
and you will never receive a response, the request just keeps "hanging".