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();
}