0

I have created a Socket.IO application and I even already got some interactivity working. But I still host static content on Apache HTTP server (localhost, XAMPP bundle). Actually when running Node.js, this is my working directory:

C:\xampp\htdocs\game>node nodeGame.js

I'd like to move it all somewhere else, probably convert it into npm package and use Node.js to serve HTML and JavaScript files to user. It would be best if I could just install some simple handler that could be passed into http. Something like:

  var http = Http.Server(require("really-simple-http-server"));
  var io = SocketIo(http);
  // Sockets below

None of the servers I found on StackOverflow seemed that simple, so which is most suitable for this purpose and how to use it?

0

1 Answer 1

1

You can use socket.io with the Express framework (using Express as your web server) and then use express.static() to serve static files:

var express = require('express');
var app = express();
var server = app.listen(3000, function() {
    console.log("server started on port 3000");
});

var io = require('socket.io').listen(server);

// set up static file serving from the public directory
app.use('/static', express.static(__dirname + '/public'));

Details on the options for serving static files with Express are here.

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

Comments

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.