Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing objects across routes

Trying out Keen.io and wondering if it's possible to NOT have to call "configure" in each file that will make a call to the Keen API?

Their docs from here: https://github.com/keenlabs/KeenClient-node show how to configure an instance which I use in one of my routes but I have a number of routes and find that I have to keep configuring.

var Keen = require('keen.io');

// Configure instance. Only projectId and writeKey are required to send data.
var client = Keen.configure({
  projectId: "<project_id>",
  writeKey: "<write_key>",
  readKey: "<read_key>",
  masterKey: "<master_key>"
});

Is there a way to "configure" just once or have I got the wrong idea?

like image 415
tommyd456 Avatar asked Jan 17 '26 02:01

tommyd456


2 Answers

Check out this answer to another question on SO. It describes a method to use app.get() and app.set() to access dependencies across the application.

After you configure client, set it in the app:

app.set('keen', client);

Then later get it back out from within any route:

var client = app.get('keen');
like image 176
Josh Dzielak Avatar answered Jan 19 '26 19:01

Josh Dzielak


I decided to configure the app inserver.js and then pass the client object to each route that requires it. This appears to work well and has tidied my code up considerably.

like image 30
tommyd456 Avatar answered Jan 19 '26 19:01

tommyd456



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!