node.js - Initializing client javascript variables from nodejs -


i trying send data client using nodejs @ same time server sending html and/or javascript. i'm pretty new web development , overlooking core concept.

here's do.

require('http');  var somevar = 'some data';  http.createserver(function(req, res){     res.writehead(200, {'content-type': 'text/plain'});     res.write(somewebpage);      res.sendthisdatatoclient(somevar);      res.end(); }).listen(4000); 

and client

var somevar = getdatasentwiththispage(); // stuff 

i did find way solve specific problem had in different way although still know how / if possible / if javascript way.

you use templating engine , send data so:

res.render('index.html', { myvar : somevar }): 

and in index.html you'd have expression evaluating myvar, example <% myvar %> in ejs, or span=myvar in jade.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -