node-cacher icon indicating copy to clipboard operation
node-cacher copied to clipboard

Any chance we can use this with Browsersync as a Middleware?

Open vviikk opened this issue 9 years ago • 2 comments

You can see that it can accept any connect compatible middlewares...

I am trying to implement it as a global, route-less middleware.

https://browsersync.io/docs/options#option-middleware

I added it as a middleware, but I'm getting a TypeError: res.header is not a function

at setHeaders (/Users/.../Sites/sample-site/node_modules/cacher/lib/Cacher.js:166:7)

I used it like:

 middleware: [
      (new cacher()).cache('seconds', 3000),
     // other working middlewares
]

vviikk avatar Nov 15 '16 16:11 vviikk

res.header is part of express's API, but there isn't any reason we couldn't use res.setHeader which is part of the node.js response class. I would totally take a PR for that.

Otherwise, you could simply create a little middleware that adds the method onto your request and response:

function addHeaderMiddleware(req, res, next) {
  req.header = function(n) {
   ...
  }
  res.header = function(n, v) {
    res.setHeader(n, v)
  } 
}

And then you should be working fine. Hope that helps!

addisonj avatar Nov 15 '16 17:11 addisonj

:) I will try this today and get back to you. Thanks a bunch!

vviikk avatar Nov 23 '16 01:11 vviikk