Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 881 Bytes

README.md

File metadata and controls

56 lines (39 loc) · 881 Bytes

cache middleware for express.js

install:

$ npm install cache-middleware-redis

usage:

const app = require("express")()

const MiddlewareCache = require("middleware-cache")

const redisConfig = { 
  url: "redis://localhost",
  ttl: 2
}

const middlewareFactory = new MiddlewareCache(redisConfig) 


/* fake auth middleware that inject account_id in req props */
app.use((req, res, next) => {
  req.account_id = req.cookies.account_id
  next()
})


const middlewareConfig = { 
  useInKey: {
    reqProps: [ "account_id" ]
  }
}

/* to can get req.params */
app.use(middlewareFactory.middleware(middlewareConfig))

app.get("/ads/:id", (req, res) => {
	res.json({ date: new Date })
})

app.put("/ads/:id", (req, res) => {

  if(req.query.error)
    res.status(400).json({ ok: false })
  else
    res.json({ ok: true })
})

TODO

  • whitelist