sharing settings/config between client and backend

I have an application using node.js backend and require.js/backbone frontend. My backend has a config/settings system, which depending on the environment (dev, production, beta) can do different things. I would like to propagate some of the variables to the client as well, and have them affect some template rendering (e.x change the Title or the URL of the pages).

What is the best way to achieve that?

I came up with a way to do it, and it seems to be working but I don't think its the smartest thing to do and I can't figure out how to make it work with requirejs optimizer anyway. What I do is on the backend I expose an /api/config method (through GET) and on the client I have the following module config.js:

// This module loads an environment config
// from the server through an API

define(function(require) {
    var cfg = require('text!/api/config');
    return $.parseJSON(cfg);
});

any page/module that needs config will just do:

var cfg = require('config');

As I said I am having problem with this approach, I can't compile/optimize my client code with requirejs optimizer since /api/config file doesn't exist in offline during optimization. And I am sure there are many other reason my approach is a bad idea.

I do the following (note that this is Jade, i have never used require.js or backbone, however as long as you can pass variables from express into your templating language, you should be able to place JSON in data-* attributes on any element you want.)

// app.js
app.get('/', function(req, res){
  var bar = {
    a: "b",
    c: Math.floor(Math.random()*5),
  };
  res.locals.foo = JSON.stringify(bar);
  res.render('some-jade-template');
});

// some-jade-template.jade
!!!
html
  head
    script(type="text/javascript"
    , src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")
    script(type="text/javascript")
      $.ready(init);
      function init(){
        var json = $('body').attr('data-stackoverflowquestion');
        var obj = JSON.parse(json);
        console.log(obj);
      };

  body(data-stackoverflowquestion=locals.foo)
    h4 Passing data with data-* attributes example