Skip to content

Instantly share code, notes, and snippets.

@spikebrehm
Created February 1, 2012 09:40
Show Gist options
  • Save spikebrehm/1716171 to your computer and use it in GitHub Desktop.
Save spikebrehm/1716171 to your computer and use it in GitHub Desktop.
Serving mustache templates using Node and jsdom
var http = require('http'),
fs = require('fs'),
jsdom = require('jsdom'),
mustache = require('mustache');
var global = this;
var trips = [
{
description: 'foobar'
},
{
description: 'asdfsdfasd adsf fsd f sd fds f sd'
}
];
var templates = {
app_view: fs.readFileSync('templates/app_view.mustache','utf-8')
};
http.createServer(function(request, response){
global.request = request;
global.response = response;
renderPage();
}).listen(8078);
function renderPage(){
jsdom.env(templates.app_view, ['./jquery.js'], function(errors, window){
var $ = window.$,
html = mustache.to_html(templates.app_view, {trips: trips});
console.log(html)
$('html').html(html)
$('#trips').bind('click', function(e){
console.log(e)
});
global.response.writeHead(200, {'Content-Type': 'text/html'});
global.response.end("<!DOCTYPE html>\n" + $('html').html());
console.log('sent!')
});
}
console.log('running')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment