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 [email protected] 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

2
  • what is the name of the .html file you want to be loaded in ./? Commented Jan 2, 2016 at 20:41
  • It's called home.html Commented Jan 2, 2016 at 21:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

further discussion around an example of using node-static can be found here: stackoverflow.com/questions/15419882/node-static-example
Sorry forgot to include the handler in the question. Upon renaming the html page it now works. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.