Skip to content Skip to sidebar Skip to footer

Nodejs - Send 200 Status Code 2 Times If Condition Is True

I to send data 2 times to client but only send second data to client if some condition is true for example: if some variable exists (from middleware, in my case: req.userId which i

Solution 1:

You can't reply twice for a single request. But you can manage the way you answer differently, for example :

router.get("/", async (req, res) => {
  var data = { foo: "bar", foo1: "bar1" };

  if (req.userId) {
    const user = await User.findById({ _id: req.userId }, "-password").lean();

    data.name = user.username;
  }

  return res.status(200).send({ data: JSON.stringify(data) });
});

Post a Comment for "Nodejs - Send 200 Status Code 2 Times If Condition Is True"