Mendi Sterenfeld
Mendi Sterenfeld

Reputation: 397

Set header with response

I'm trying to send a token from the client to fetch that on my node.js server. then I want the response of the server to set this token in the header so that each further request from the client will send along with the token.

I'm using vanilla javascript for the front end and express on my backend

here's the fetch request:

data = {
        id: idToken
      };
      fetch("http://localhost:5000/check", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
      }).then(response => {
        console.log(response);
      });

this is the server code:

app.post("/check", (req, res) => {
  console.log(req.body.id);

  res.setHeader("token", req.body.id);
  res.redirect("/");
});

where am I wrong?

Upvotes: 1

Views: 1483

Answers (1)

Quentin
Quentin

Reputation: 944443

Headers are a generic way to send metadata with an HTTP message. They aren't a generic way to persist session data.

The tool designed to do what you want is a cookie.

Use res.cookie() to set a cookie and the cookie-parser middleware to read it back.

Consider using cookie based sessions instead.

Upvotes: 1

Related Questions