1

I saw this post on how to include an external html file and it works well.
Q: Is it possible to pass in variables from node into the html file?
node:

var http = require('http'), fs = require('fs');

var mytitle = 'Bold Title';//<< pass this into the html markup

fs.readFile('./markup.html', function (err, html) {
    if (err) {
            throw err; 
    } 
    exports.serve = function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'})
        res.write(html);  
        res.end(); 
    };      

});

html:

<!DOCTYPE HTML>
<html lang="en-US">
<head></head>
<body>
    <p> [mytitle] </p>
</body>
</html>


The reason: the html markup will be quite large and if i pass the entire dom as a variable it gets messy and difficult to read.
Is there a better way to do this?

function page(req, res, pre, cb) {

    var mytitle = 'Bold Title';

    var content = '<html>\
    <head></head>\
    <body>\
    <p>'+mytitle+'</p>\
    </body>\
    </html>';

    res.writeHead(200, {'Content-Type': 'text/html'})
    res.end(content);
    cb();
}
Community
  • 1
  • 1
t q
  • 4,593
  • 8
  • 56
  • 91
  • It's possible a template module like doT might be a good solution. – Matthew Bakaitis Apr 30 '13 at 17:48
  • You should be using some templating language, like Jade or EJS. – Dogbert Apr 30 '13 at 17:48
  • i see, how is `ember.js`? – t q Apr 30 '13 at 17:49
  • @tq, what kind of site are you building? ember.js is a full blown app framework. – Dogbert Apr 30 '13 at 17:58
  • @Dogbert building a a moderately sized web app. if i went with ember do you know if that would replace Jade or other templates? – t q Apr 30 '13 at 18:00
  • 1
    @tq, what kind of app if you don't mind me asking? Ember.js is suited for single page apps, highly interactive, like, say Google Reader. Ember.js uses http://handlebarsjs.com/ as its default templating language, so Handlebars would essentially replace Jade if you use Ember.js – Dogbert Apr 30 '13 at 18:15
  • @Dogbert a `cms` for building web pages and a smaller version of something like criagslist – t q Apr 30 '13 at 18:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29206/discussion-between-t-q-and-dogbert) – t q Apr 30 '13 at 18:30

0 Answers0