0

I have an express middleware that reads a query param and sets a "flag" in request headers if it's present. I want to use the presence of this flag in other middlewares which handle setting cookies.

However, passing the query param from the browser only sets it in that particular request. When loading the page, there are several other requests from other sources to load content, and they don't include that header, so the other middlewares end up firing (which I don't want).

What are some appropriate ways to solve this problem? I essentially need my "flag" to persist across requests, besides using session.

1sentenced
  • 53
  • 6
  • You could make all of your requests use the same module for making the request itself (e.g. a class or just a function of your own), and make that module set the header in question. E.g. if you're using Axios, you can [create an instance](https://github.com/axios/axios#creating-an-instance) with default headers, and all requests made using that instance include that header. Alternatively, set a cookie. That's what they're for. – cbr Jul 27 '22 at 20:15

1 Answers1

0

Sounds like you need to set a cookie instead, and parse it with cookie-parser middleware.

This answer is probably what you're looking for.

Visvamba Nathan
  • 135
  • 1
  • 6