Reputation: 59345
Is this the correct way to have asynchronous functions only run once within express? I don't want this function to have to run on every route. I'm looking for an express-friendly way to do this.
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()
})
})
Upvotes: 0
Views: 38
Reputation: 203241
Just for reference (this is not a generic solution, @Bergi's is), in this particular case you can use require()
, which has two useful features:
The middleware would look like this:
app.use(function(req, res, next){
req.packageJson = require('./package.json');
return next();
});
The first request will take a small performance impact, as require()
is synchronous, but for subsequent requests the data will be read from memory automatically.
Upvotes: 1
Reputation: 664346
I wouldn't even run the asynchronous function from within the first call to that route, but just do
var packageJson = fs.readFileAsync("./package.json", "utf8").then(JSON.parse);
app.use(function(req, res, next){
if (req.packageJson)
return next();
else
packageJson.then(function(data){
req.packageJson = data;
return next();
});
});
But don't forget proper error handling, if that packageJSON
promise is rejected your route might hang infinitely.
Upvotes: 2