0

Having this script:

const got = require("got");
const getStream = require("get-stream");
const app = require("express")();

async function httpGet() {
  console.log("getting response");
  let targetUrl = "http://localhost:3000/api";
  let gotOptions = {
    method: "get",
    headers: { "content-type": "application/json" },
    body: undefined,
    retries: 0
  };
  let response = await new Promise(async (resolve, reject) => {
    let stream = got.stream(targetUrl, gotOptions);
    stream.on("error", async error => {
      try {
        resolve(stream);
      } catch (err) {
        reject(err);
      }
    });
    stream.on("response", async res => {
      try {
        resolve(await getStream(res));
      } catch (err) {
        reject(err);
      }
    })
  });
  return response;
}

async function apiMiddleware(req, res, next) {
  try {
    const response = await httpGet();
    console.log(response);
  } catch (e) {
    throw new Error("some error");
  }
  next();
}

app.use("/", [apiMiddleware]);

app.get("/api", (req, res) => {
  res.json({ data: "some output" });
});
app.get("/someapp", (req, res) => {
  res.end();
});

app.listen(3000, () => {
  console.log("listening on port 3000");
});

Will (upon visiting "localhost:3000/someapp") console.log

getting response
getting response
getting response
getting response
getting response
getting response
getting response
getting response
getting response
getting response
getting response
...

When debugging, the promise will always throw me at the start of httpGet(), so what is going on?

milanHrabos
  • 2,010
  • 3
  • 11
  • 45
  • Just remove the resolve(stream); on error and check `stream.on("error", async error => { reject(err); });` – raghava patnam Jul 13 '22 at 22:59
  • [Never pass an `async function` as the executor to `new Promise`](https://stackoverflow.com/q/43036229/1048572)! – Bergi Jul 13 '22 at 23:03

1 Answers1

0

what is going on?

You've registered the apiMiddleware for all routes (/), that includes the /api route. So whenever you are requesting anything from the server, apiMiddleware() is getting called, which will - wait for it - make a http request to http://localhost:3000/api! This leads to an infinite recursion. And, since you're waiting for the response before sending a response, probably also to quickly running out of memory.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • So I cannot basically request anything from one server (which has middlewares set on "/") but since that would create a loop. Well the api call should be done to another server anyway (since it is api), but thanks for clarification – milanHrabos Jul 14 '22 at 08:35