Search code examples
javascriptstrapi

Strapi backend broken when enable custom middleware


I created a custom middleware that makes the api & admin unreachable when I enable it.

The middleware is pretty simple, it adds a request id to incoming request on the server:

const { createNamespace } = require('cls-hooked');
const { v4: uuidv4 } = require('uuid');

const loggerNamespace = createNamespace('logger');

module.exports = (strapi) => {
  return {
    initialize() {
      strapi.app.use((ctx, next) => {
        const reqId = ctx.request.get('X-Request-Id') || uuidv4();

        ctx.response.set('X-Request-Id', reqId);

        loggerNamespace.run(() => {
          loggerNamespace.set('requestId', reqId);

          next();
        });
      });
    },
  };
};

It's enable using the config file ./config/middleware.json:

module.exports = {
  settings: {
    addRequestId: {
      enabled: true,
    },
  },
};

Then, when enabled, calling an API endpoint or trying to connect to admin results in 404 Not Found.

I use strapi 3.6.8 with node 14.18.1.

Any idea why?

PS: I suspected cls-hooked to be the culprit but removing it for testing with an anemic middleware doesn't work either.


Solution

  • @Salvino put me on the right track suggesting that I should await the execution of next.

    Digging in the code of cls-hooked, I found the method runPromise that is similar to run and returns a Promise that can be awaited. It solved the issue.

    Fixed code:

    const { createNamespace } = require('cls-hooked');
    const { v4: uuidv4 } = require('uuid');
    
    const loggerNamespace = createNamespace('logger');
    
    module.exports = (strapi) => {
      return {
        initialize() {
          strapi.app.use(async (ctx, next) => {
            const reqId = ctx.request.get('X-Request-Id') || uuidv4();
    
            ctx.response.set('X-Request-Id', reqId);
    
            await loggerNamespace.runPromise(async () => {
              loggerNamespace.set('requestId', reqId);
    
              await next();
            });
          });
        },
      };
    };