Giter Club home page Giter Club logo

Comments (4)

kwhitley avatar kwhitley commented on May 18, 2024 1

from apicache.

kwhitley avatar kwhitley commented on May 18, 2024

@IgorKhromov Remember, the cache middleware (including toggle middleware shown here) gets called at the last moment, before res.end, rather than at the entry to the route like typical middleware (it does this by hijacking the res.end to delay its execution). By this point, you would have set 403, 404, 500 statuses depending on the response.

This particular middleware toggle simply returns true if the statusCode === 200, so if that returns true, the middleware continues on to cache the response.

That help at all? Test it out by setting the statusCode to say, 400 within your route and see if it caches. It shouldn't! If it does, please reopen this as a bug and I'll resolve and add test coverage.

Cheers!

from apicache.

caprica avatar caprica commented on May 18, 2024

I think I'm also missing something here...

const router = require('express').Router()

const apicache = require('apicache')
const cache = apicache.middleware

const statusOkOrNotFound = (req, res) => {
    console.log("check status " + res.statusCode)
    return res.statusCode === 200 || res.statusCode === 404
}

const cacheValid = cache('5 minutes', statusOkOrNotFound)

router.get('/api/test', cacheValid, (req, res) => {
    console.log("API request, send fake error")
    res.status(500).send("fake error")
    return
}

When I execute this request, I see this trace in unexpected order:

check status 200
API request, send fake error

And this response:

HTTP/1.1 500 Internal Server Error
x-powered-by: Express
cache-control: max-age=300

The only difference I see between this and your example is I do:

const router = require('express').Router()
router.get(...)

And your example shows:

const app = require('express')
app.get(...)

from apicache.

sdomagala avatar sdomagala commented on May 18, 2024

I'm having the same issue, check is triggered long before the call is done - therefore it caches every response

node -v
v6.10.3
npm -v
3.10.10

from apicache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.