Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js - using weld with express?

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.

like image 250
7zark7 Avatar asked Sep 28 '11 07:09

7zark7


1 Answers

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);
like image 157
Farid Nouri Neshat Avatar answered Oct 23 '22 14:10

Farid Nouri Neshat