Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'x-apicache-bypass' of undefined #77

Closed
mwkrieger opened this issue Mar 23, 2017 · 1 comment
Closed

Cannot read property 'x-apicache-bypass' of undefined #77

mwkrieger opened this issue Mar 23, 2017 · 1 comment

Comments

@mwkrieger
Copy link

Hi,

Having a bit of an issue when trying to implement. My sample code is as follows:

var express = require('express');
var apicache = require('apicache');
apicache.options({ debug: true });
var cache = apicache.middleware();
var router = express.Router();

router.get('/test', cache('1 hour'), function (req, res) {
//code in here
};

When the app loads, I get the error 'Cannot read property 'x-apicache-bypass' of undefined'.

Any idea on what I might be missing?

@kwhitley
Copy link
Owner

Hey @mwkrieger - you just need to not immediately execute the middleware function during your assignment. It should be:

var cache = apicache.middleware;

...instead. It's just a reference to the built in middleware function that you would pass to your route. There are lots of ways to write this of course, and if you wanted a single time duration through your entire routes, you could do something like:

var cached = apicache.middleware('1 hour');

router.get('/test/', cached, function(req, res) {
  // code here
})

notice that middleware only gets executed once in both cases (just in different spots). The return value of that execution is a true middleware function(req, res, next) signature, so if you accidentally execute it twice, there are no longer request/response objects captured (which apicache uses internally of course)

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants