Caching files in node.js

dave_pickard
Participant III

We're making HTTPS calls from Node.js that require use of a client certificate. The certificate/key & associated password are read in from file and this all works. My concern though is having to read in the files for every request and the question is what is the best way to cache this.

Would using the apigee-access module to access the Edge distributed cache be the appropriate thing to do in these circumstances? Or would it be better to use one of the Node file caches that are available?

Solved Solved
0 5 2,656
1 ACCEPTED SOLUTION

When i did a similar implementation - i just loaded my cert during initialization, and not in the request handler. So the request handlers need not load the files everytime. But the downside to that is, it is permanently loaded.

View solution in original post

5 REPLIES 5

When i did a similar implementation - i just loaded my cert during initialization, and not in the request handler. So the request handlers need not load the files everytime. But the downside to that is, it is permanently loaded.

@Mukundha Madhavan - do you know if there is any documentation on where / how initialisation is done? I'm struggling to find any.

the script will be run when the proxy is deployed, so behavior is similar to how you run a simple http server in node -

so for eg, you can do something like

cert = fs.readFileSync('mycert.pem')

app.get('/foo',function(req,res){
  //use cert here
})
app.post('/bar',function(req,res){
//use cert here
})
app.listen(8080)

the request handlers are only called based on the incoming request, however the script itself is run during deployment [so cert will be set when you deploy the proxy],

Dave, just to elaborate on what Mukundha suggested. All the code in the nodejs script will run when the proxy is deployed. The .get and .post handlers get registered at deployment time, though of course that request handling logic only *runs* when the appropriate request comes in. As a small change to what Mukundha showed, you can also do something like this:
var cert = fs.readFileSync('mycert.pem'); // runs at deploy time

// register the .get handler at deploy time
app.get('/foo',function(req,res){ 
  // use cert here; this happens only at the time a GET request arrives
});

// register the .get handler at deploy time
app.post('/bar',function(req,res){
  // use cert here; this happens only when a POST request arrives
});

app.listen(8080); // happens at deploy time

Thanks guys - helpful replies.