How can I bootstrap models from express js into backbone at page load?

I have some data in a mongodb database and want to pass it to a backbone collection when I load the home page. One way of doing this would be to set up a node route like this:

exports.index = function(req, res){

  db.users.find(function(err, docs) {
    var docs_string = JSON.stringify(docs);
    res.send(docs_string);
  };
};

But this won't work because it won't render my jade template that pulls in the backbone code, it simply shows the JSON in plain text.

Alternatively, I could render my jade template passing in the data as a variable to jade:

exports.index = function(req, res){

  db.users.find(function(err, docs) {
    var docs_string = JSON.stringify(docs);
    res.render('index', {
      title: "Data",
      docs_string: docs_string
    })
  });
};

Then in the jade template, have a script like this to add the users to my user collection:

script
  var docs = !{docs_string};
  var users = new app.Users();
  _.each(docs, function(doc) {
      var user = new app.User(doc);
      users.add(user);
  })

But this seems wrong, since I don't really want to pass the data to the jade template, I want to pass it to a backbone collection. Also, with this solution I don't know how to then include an underscore template (on the backbone side of things) into the page rendered by jade on the server side.

What is the standard way of passing data from a node server to a backbone collection?

Assuming your data is an object, you should convert it to string using JSON.stringify() and then insert in a page inside script tag, so your resulting HTML looks like this (I don't use Jade):

<script>
    var data = {...}; // in template instead of {...} here should be the instruction to insert your json string
</script>

Then when the page loads, your script will be executed and the data will be available as a global variable in the browser so you can initialise backbone collection using it. This all is a good idea only to bootstrap your data on the first page load (to avoid extra request) and then use API to request data for this and other pages.

Check out Steamer, a tiny node / express module made for this exact purpose.

https://github.com/rotundasoftware/steamer