https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally suggests that three.js examples be served by a local server. The python SimpleHTTPServer works fine for me, except that I need to run it in the directory above the examples dir in the three.js repository clone.
Now I'm trying to serve up the same example using httpServer in node.js. I could use one of the node versions of SimpleHTTPServer, but I need a httpServer object to pass data from the server to the webgl browser example via socket.io. So I took the socket.io example and tried the following server.js to be run using node in the directory above examples.
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/examples/webgl_interactive_voxelpainter.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Net is that on 127.0.0.1:8080 with node I can't see the three.js example. The code doesn't work even if I remove all references to socket.io, indicating it's something about html.
What am I missing? The html file is being read properly because I don't get a callback error.
I noticed that the python server lists the directory as html links in the browser. I click examples to see the html files, then click the html file and it works fine. So I tried running the 'node server.js' one directory level up, with just about every combination of forward and backward slashes, root directory references,... to no avail.
I'm not hung up on pure httpServer. If express or something else works with socket.io and three.js, I'll board that train.
Use connect framework, makes your job easier.
var connect = require('connect');
var app = connect()
.use(connect.static('<your directory>'))
.use(function(req, res){
res.end();
})
.listen(8080);