Feature Toggling Middleware

Demonstrate the use of middleware to enable or disable features dynamically without redeploying the app.
const express = require('express');
const app = express();
const PORT = 3000;

// Feature toggles object simulating a dynamic store or config
const featureToggles = {
    newFeature: true

// Middleware to check feature toggle
const featureToggleMiddleware = (req, res, next) => {
    const featureName = req.query.featureName;
    if (featureToggles[featureName]) {
    } else {
        res.status(403).send('Feature is disabled');
This code sets up an Express application and a feature toggle configuration. It defines the featureToggleMiddleware which will check if a feature is enabled based on query parameter before proceeding with the request.
app.use((req, res, next) => {
    console.log(`Request received: ${req.method} ${req.url}`);

// Applying the feature toggle middleware globally, it affects all routes
This code snippet demonstrates how to use middleware for logging requests and applies the featureToggleMiddleware globally to the Express application.
app.get('/new-feature', (req, res) => {
    res.send('New feature is accessible because the toggle is enabled.');

app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
Here we define a route that would be affected by the featureToggleMiddleware. The app listens on the specified PORT, and the response is sent only if the feature toggle for 'new-feature' is enabled.