0

My current project is using Node for both frontend and backend and ExpressJS as the middleware.

I have a requirement where I need a feature toggling implementation to introduce some new features in my application. I am using a url parameter, e.g. &featureToggle=true to determine if the code of execution would be the new one or the existing.

Now I have parts in frontend and backend both which need to be changed based on the feature toggle. In the backend I can get the query object separately and extract the url param, similarly also in the frontend module.

Is there a way in which I can use Express to intercept the query param, set a variable value to either true or false based on the feature toggle, and which could be used across both the frontend and backend modules?

Kunwar
  • 67
  • 3
  • 7
  • `app.use(() => {})` middleware? https://expressjs.com/en/guide/using-middleware.html – Marc Apr 08 '22 at 10:08

1 Answers1

0

with express you can use req.query which gathers the query string sent in the request. You could pass it like this:

localhost:9000/path?&featureToggle=true

the ? is important it tells express that you are creating a query.

if you then place it into a variable:

const query = req.query

you would get the following output:

{ featureToggle: 'true' }

so as you can see it is returning an object. you can check it like so:

if(req.query.featureToggle === 'true'){
  runSomeCode();
};

or in your case if you want to run some kind of middleware:

router.get('/', (req, res, next) => {
  if(req.query.featureToggle === 'true'){
    return next(toggle)
  }
};
Dylan L.
  • 1,243
  • 2
  • 16
  • 35