NodeJS express application set cache at HTTP request

 Follow the guideline to cache for http request:


const setCache = (req, res, next) =>{

    const period = 5*60;
 
    if(req.method == 'GET')
    {
        res.setCache('Cache-control', `public, max-age=${period}`)

    }else{
        res.setCache('Cache-control', 'no-store')
    }

    next()
}

module.exports = {
    setCache
}


set as middleware 

app.use(setCache)

Post a Comment

0 Comments