to communicate between 2 pages using nowjs

32 views
Skip to first unread message

maxi

unread,
Sep 4, 2011, 5:21:16 PM9/4/11
to nowjs
How to make communicate two pages on the same server?

Example:

var html = require('fs').readFileSync(__dirname+'/helloworld.html');
var server = require('http').createServer(function(req, res){
res.end(html);
});

server.listen(8080);

I'd like to open another connection with new page 'helloworld1.html'
and syncing 'now Object' in both.

is it possible ?

thanks again

Maxi

Eric Zhang

unread,
Sep 7, 2011, 3:36:36 AM9/7/11
to no...@googlegroups.com
Hi Maxi,

You simply have to serve the other HTML page any way you choose. As long as the server is the same, clients will connect to NowJS using the same `now` object.


Eric

maxi

unread,
Sep 7, 2011, 1:37:49 PM9/7/11
to nowjs
Hi Eric,

i tried with this :

var fs = require('fs');
var server = require('http').createServer(function(req, response){
fs.readFile(__dirname+'/game.tpl', function(err, data){
response.writeHead(200, {'Content-Type':'text/html'});
response.write(data);
response.end();
});
});

var server1= require('http').createServer(function(req, response){
fs.readFile(__dirname+'/report.tpl', function(err, data){
response.writeHead(200, {'Content-Type':'text/html'});
response.write(data);
response.end();
});
});

server.listen(8090);
server1.listen(8090);

but I received an error :

EADDRINUSE, Address already in use
Error: EADDRINUSE, Address already in use
at Array.<anonymous> (net.js:1116:7)
at EventEmitter._tickCallback (node.js:126:26)


Maxi

Eric Zhang

unread,
Sep 7, 2011, 1:43:45 PM9/7/11
to no...@googlegroups.com
Maxi,

You don't need seperate servers to serve multiple html files. Instead you need to parse the response and then serve files conditionally.

Consider using `express` http://expressjs.com/ as it abstracts away all of the difficulty of serving files and is compatible with NowJS



Eric

maxi

unread,
Sep 7, 2011, 3:00:32 PM9/7/11
to nowjs
thanks Eric,
I solved it.

var server = require('http').createServer(function(req, response){
fs.readFile(__dirname+'/game.tpl', function(err, data){
response.writeHead(200, {'Content-Type':'text/html'});
response.write(data);
response.end();
});

fs.readFile(__dirname+'/report.tpl', function(err, data){
response.writeHead(200, {'Content-Type':'text/html'});
response.write(data);
response.end();
});
});


Maxi

On 7 Set, 19:43, Eric Zhang <e...@nowjs.com> wrote:
> Maxi,
>
> Youdon't need seperate servers to serve multiple html files. Instead you
> need to parse the response and then serve files conditionally.
>
> Consider using `express`http://expressjs.com/as itabstracts awayall of
Reply all
Reply to author
Forward
0 new messages