Pass configuration to controller
Asked Answered
E

3

6

I am building a node.js app that will upload files to my S3 bucket using knox. I am able to interact with S3 as expected, but I would like to make my controller to receive configuration so I can dynamically construct my client with configuration values.

My questions is how do I get configuration paramters down the call stack to my controller without being careless?

Disclaimer: I am rather new to Node.js so it could be simply my lack of knowledge in the difference between exports. and module.exports.*

Here is an example of how the interaction works with my code:

app.js

...
config = require('./config/config')['env'];
require('./config/router')(app, config);
...

router.js

module.exports = function(app, config) {
...
  var controller = require('../app/controllers/home'); //Is there a way for me to pass config here?
  app.post('/upload', controller.upload); //Or here?
...
}

home.js

var knox = require('knox');

var client = knox.createClient({ ... }); //I want to use config.key, config.secret, etc instead of hard-coded values
...
exports.upload = function(req, res) {
  //Use client
}
...
Ethnology answered 28/1, 2013 at 22:15 Comment(0)
E
8

Try doing something like this...

var config = require('./config/config')['env'];

// The use function will be called before your 
//  action, because it is registered first.
app.use(function (req, res, next) {

  // Assign the config to the req object
  req.config = config;

  // Call the next function in the pipeline (your controller actions).
  return next();

});

// After app.use you register your controller action
app.post('/upload', controller.upload); 

And then in your controller action...

exports.upload = function(req, res) {

  //Your config should be here...
  console.log(req.config);

}

Ps. I can not try it right now, but I solved a similar issue like this.

Eighteenth answered 28/1, 2013 at 22:41 Comment(1)
I have a cluster of Node.js worker processes, each running an Express server instance and I used this answer to pass a worker ID into the request handlers for each request. This helps with things like generating unique IDs and logging.Cila
H
1

You can pass the configuration in as a parameter to you controller

Controller

// controller.js file
module.exports = function(req, res, config) {
  console.log('config parameter passed to controller', config);
  res.end('config passed')
}

App

// index.js file with the express app
var controller = require('./controller');
var config = {
  key1: 'foo'
};
var express = require('express');
var app = express();
var port = 3000;
app.get('/', function(req, res){
  controller(req, res, config);
});
app.listen(port);

console.log('app listening on port', 3000);

Demo

You can check out the github repo for a complete example

Hawn answered 30/1, 2013 at 2:38 Comment(1)
Thanks for the answer. The problem is this is all in one file. My desire was to keep things separate thus my problem. Bart's approach got me where I needed to be.Ethnology
M
0

Alternative approach if you want to call multiple functions from one single route, this will do it.

Route

var users = require('../controllers/users');
 app.route('/login').post(function(req, res){
   if(users.authenticate()){
     console.log('valid user');
     if(users.createUser())
     {
       console.log('user created');
     }
   }
});

Controller

exports.authenticate = function(req, res, next) {
   return true;
};
exports.createUser = function(req, res, next) {
   return true;
};
Martens answered 24/12, 2014 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.