express-cache-controller icon indicating copy to clipboard operation
express-cache-controller copied to clipboard

Middleware unable to be used by Express router

Open mikecbrant opened this issue 6 years ago • 1 comments

Was trying to use this module to apply different default cache control settings on two Express routers mounted on an app, and it seem this use case is not supported (perhaps because of (odd?) way that the modules exposes a function the returns a named function?)

My code looked like:

const cacheControl = require('express-cache-controller');
...
apiRouter.use(cacheControl({ [options] });
...
mainRouter.use(cacheControl({ [options] });

This did not work and there was not any sort of error triggered. My routers just simply did not apply the correct headers.

I tried other alternatives like:

const cacheControlInit = require('express-cache-controller');
...
apiRouter.use(cacheControlInit ({ [options] });
...
mainRouter.use(cacheControlInit ({ [options] });

and:

require('express-cache-controller')({ [options] });
...
apiRouter.use(cacheControl);
...
mainRouter.use(cacheControl);

but wasn't able to get to work until finally just doing:

const cacheControl = require('express-cache-controller');
...
app.use(cacheControl({ [options] });

Figured I would open an issue, as it would be unexpected behavior IMO middleware to not work on a router.

I also think it would be extremely helpful to include information on how to properly require the module in your documentation. I saw there was an old closed issue about this, but the documentation doesn't show this. It would have probably saved some time in me fiddling around with various approaches to including this module.

mikecbrant avatar Jul 27 '18 21:07 mikecbrant