# Caching files in node.js

**URL:** <https://discuss.google.dev/t/caching-files-in-node-js/11502>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [August 26, 2015, 2:19pm UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502 "2015-08-26T14:19:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [August 26, 2015, 2:19pm UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/1 "2015-08-26T14:19:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gnanasekaran](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gnanasekaran](https://discuss.google.dev/u/gnanasekaran)\
**Post date:** [August 31, 2015, 6:21pm UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/2 "2015-08-31T18:21:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 1, 2015, 7:41am UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/3 "2015-09-01T07:41:57Z")

</div>

[@Mukundha Madhavan](https://community.apigee.com/users/954/mukundha.html) - do you know if there is any documentation on where / how initialisation is done? I’m struggling to find any.

---

<div class="post-metadata">

**Author:** ![gnanasekaran](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gnanasekaran](https://discuss.google.dev/u/gnanasekaran)\
**Post date:** [September 1, 2015, 1:39pm UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/4 "2015-09-01T13:39:36Z")

</div>

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

```auto
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],

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 2, 2015, 5:16pm UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/5 "2015-09-02T17:16:40Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 3, 2015, 6:57am UTC](https://discuss.google.dev/t/caching-files-in-node-js/11502/6 "2015-09-03T06:57:23Z")

</div>

Thanks guys - helpful replies.
