I'm new to node.js, and attempting to use weld to render templates on the server-side and using express as the router.
However the examples for node.js doesn't show serving the content, and am fuzzy on how this would work with express:
var fs = require('fs'),
jsdom = require('jsdom');
jsdom.env(
'./test.html',
['./jquery.js', './weld.js'],
function(errors, window) {
var data = [{ name: 'hij1nx', title : 'code slayer' },
{ name: 'tmpvar', title : 'code pimp' }];
window.weld(window.$('.contact')[0], data);
}
);
Help or example would be appreciated.
I think something like this would work. Haven't tested though.
var fs = require('fs'),
jsdom = require('jsdom'),
app = require('express').createServer();
app.get('/', function(req, res) {
jsdom.env('./test.html', ['./jquery.js', './weld.js'], function(errors, window) {
var data = [{
name : 'hij1nx',
title : 'code slayer'
}, {
name : 'tmpvar',
title : 'code pimp'
}];
window.weld(window.$('.contact')[0], data);
res.send(window.document.innerHTML); //after the welding part we just send the innerHTML
window.close(); // to prevent memory leaks of JSDOM
});
});
app.listen(3001);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With