logzly. DevCraft

Feature Flags in Node.js: Quick Start Guide with Express

Read this article in clean Markdown format for LLMs and AI context.

Tired of pushing risky features and scrambling for midnight hotfixes? Learn how to add feature flags in Node.js so you can toggle features on or off without redeploying.
By the end of this guide you’ll have a working flag system, a reusable middleware, and clear steps to scale to a dynamic store.

Early on I would roll out new code straight to production, hoping everything would be fine. One time a small UI tweak broke the checkout flow and I forced a midnight hotfix. That panic taught me I needed a way to flip a feature on or off without touching the server.

How to Implement Feature Flags in Node.js with Express

The simplest path is to pick a lightweight library—toggle works great for side projects, though you can swap in LaunchDarkly later. First install the package, then create a tiny JSON file that holds your flags as true or false.

  1. npm install toggle (or your preferred flag library)
  2. Create flags.json with { "newFeature": false }
  3. In app.js, load the JSON and attach it to every request:
const flags = require('./flags.json');
app.use((req, res, next) => {
  req.flags = flags;
  next();
});

Now wrap the routes you want to guard with an if check that reads req.flags.

app.get('/checkout', (req, res) => {
  if (req.flags.newFeature) {
    // run new checkout logic
  } else {
    // run existing checkout logic
  }
});

Testing is just a matter of flipping the value in flags.json and restarting the server—no redeploy needed. Over time you can migrate to a dynamic store (Redis, a database, or a SaaS service) while keeping the same middleware pattern.

Why This Approach Works

  • Zero‑downtime toggles: change a file, restart, and the new flag takes effect instantly.
  • Clear rollout strategy: start with one flag, observe metrics, then add more as confidence grows.
  • Minimal overhead: the middleware adds only a tiny object to each request, keeping performance impact negligible.

Scaling Beyond the File‑Based Store

When you’re ready for more control, replace the static JSON loader with a function that fetches flags from Redis or an external API. The middleware signature stays the same, so your route guards need zero changes.

Give it a try on a low‑risk feature or a side project—you’ll see how much safer and faster shipping becomes when you can flip a switch instead of praying for a perfect deploy. If this helped you, consider sharing it with a teammate who could benefit from a simpler feature toggle rollout strategy for Node.js.

Reactions
Do you have any feedback or ideas on how we can improve this page?