Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js - using weld with express?

node.js - using weld with express?

Problem

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.

Problem courtesy of: 7zark7

Solution

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);
Solution courtesy of: Farid Nouri Neshat

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js - using weld with express?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×