Question

I have a bit of an issue. I'm trying to create a dynamic web app using node.js/express.js/now.js. I've done everything as shown in the small sample code at http://nowjs.com/download , with no success, the client-side now.js script hosted properly, but now.ready(..) never fires. The only differences are that I use express and my server which is used to initialze now.js is https.

Do you have any ideas which could cause it not to work?

server side:

var server = express.createServer(..);
..
server.listen(port, function() {..});
var nowjs = require('now');
var everyone = nowjs.initialize(server);
everyone.now.log = function(msg) { console.log(msg); }

client side:

<script type="text/javascript" src="/nowjs/now.js"></script>
<script type="text/javascript">
    now.ready(function() { now.log('asd'); alert('asd'); });
</script>

Any help would be highly appreciated!

Best, Kornel

Was it helpful?

Solution

Well, found the answer. Long answer: now.js has an issue when determining the communication port on which socket.io should communicate. This issue seems only to appear when using default https port (443).

I've found two solutions, the ugly one: https://groups.google.com/forum/?fromgroups=#!topic/nowjs/8cO9D77tN2o

Basically you need to edit the source code of now.js at now/lib/fileServer.js and replace

var hostPort =  options['port'] || host[1] || '80';

with

var hostPort =  options['port'] || host[1] || ((request.headers.referer.split(':')[0] === 'https') ? '443' : '80');

The nicer one is to set port options to socket.io. Lucky us, this is supported by now.js:

var everyone = nowjs.initialize(server, {port: port, socketio: {transports: ['xhr-polling', 'jsonp-polling']}});

I hope that this will help others having the same issue and also hope that this behavior will be fixed later in now.js.

Best regards: Kornel

OTHER TIPS

Running latest version of node and now on OSX, with Safari.

server.js

var html = require('fs').readFileSync(__dirname+'/index.html');
var httpServer = require('http').createServer(function(req, response) { 
    /* Serve your static files */ 
    response.end(html);
})
httpServer.listen(8080);

var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);

console.log('done');
everyone.now.logStuff = function(msg){
    console.log(msg);
}

index.html

<script type="text/javascript" src="http://localhost:8080/nowjs/now.js"></script>

<script type="text/javascript">
  now.ready(function(){
    // "Hello World!" will print on server
    now.logStuff("Hello World!");
  });
</script>
done..

Start the server:

node server.js

Open your browser:

http://localhost:8080
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top