|
| 1 | +var fs = require('fs'), |
| 2 | + path = require('path'), |
| 3 | + express = require('express'), |
| 4 | + dust = require('dustjs-linkedin'), |
| 5 | + q = require('q'); |
| 6 | + |
| 7 | +// Define a custom `onLoad` function to tell Dust how to load templates |
| 8 | +dust.onLoad = function(tmpl, cb) { |
| 9 | + fs.readFile(path.join('./views', path.resolve('/', tmpl + '.dust')), |
| 10 | + { encoding: 'utf8' }, cb); |
| 11 | +}; |
| 12 | + |
| 13 | +var app = express(); |
| 14 | + |
| 15 | +var position; |
| 16 | + |
| 17 | +app.get('/', function (req, res) { |
| 18 | + dust.render('index', {}, function(err, out) { |
| 19 | + res.send(out); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +app.use(function(req, res, next) { |
| 24 | + console.log('\n\nBeginning the render of', req.path); |
| 25 | + next(); |
| 26 | +}); |
| 27 | + |
| 28 | +app.get('/streaming', function(req, res) { |
| 29 | + position = 1; |
| 30 | + dust.stream('hello', { |
| 31 | + 'wait': function(chunk, context, bodies, params) { |
| 32 | + var delayMilliseconds = parseInt(params.delay, 10) * 1000; |
| 33 | + // Returning a Promise-- Dust will wait for the promise to resolve |
| 34 | + return q(position++).delay(delayMilliseconds) |
| 35 | + .then(function(position) { |
| 36 | + console.log('Rendering', params.name, 'which started in position', position); |
| 37 | + }); |
| 38 | + } |
| 39 | + }).pipe(res) |
| 40 | + .on('end', function() { |
| 41 | + console.log('Done!'); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +app.get('/rendering', function(req, res) { |
| 46 | + position = 1; |
| 47 | + dust.render('hello', { |
| 48 | + 'wait': function(chunk, context, bodies, params) { |
| 49 | + var delayMilliseconds = parseInt(params.delay, 10) * 1000; |
| 50 | + // Returning a Promise-- Dust will wait for the promise to resolve |
| 51 | + return q(position++).delay(delayMilliseconds) |
| 52 | + .then(function(position) { |
| 53 | + console.log('Rendering', params.name, 'which started in position', position); |
| 54 | + }); |
| 55 | + } |
| 56 | + }, function(err, out) { |
| 57 | + console.log('Done!'); |
| 58 | + res.send(out); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +app.listen(3000, function () { |
| 63 | + console.log('Using dust version', dust.version); |
| 64 | + console.log('Visit http://localhost:3000'); |
| 65 | +}); |
0 commit comments