Can anyone provide guidance on how to ensure asynchronous functions are only executed once within an Express application? I'd like to avoid running this function on every route and am seeking a more express-friendly approach.
var packageJson = false
app.use(function(req, res, next){
if(req.packageJson) return next()
if(packageJson){
req.packageJson = packageJson
return next()
}
return fs.readFileAsync("./package.json", "utf8")
.then(JSON.parse)
.then(function(data){
packageJson = data
req.packageJson = data
return next()
})
})