0

I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/

the full code for this problem can be seen in the link above.

I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages

npm install socket.io@0.9.10 node-static

and setting up the serverside js as instructed

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');

var fileServer = new nstatic.Server('./');

app.listen(8080);

I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.

enter image description here

Community
  • 1
  • 1
Pudding
  • 533
  • 1
  • 6
  • 23

1 Answers1

1

The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');

app.listen(8080);
var file = new nstatic.Server('./');

function handler(request, response) {
    request.addListener('end', function () {
        file.serve(request, response);
    }).resume();
}

console.log('started')

Note also that the default file to serve to responses at / is index.html.

Catalyst
  • 3,143
  • 16
  • 20
  • further discussion around an example of using node-static can be found here: http://stackoverflow.com/questions/15419882/node-static-example – Catalyst Jan 03 '16 at 04:52
  • Sorry forgot to include the handler in the question. Upon renaming the html page it now works. Thanks for your help. – Pudding Jan 03 '16 at 18:54