使用Node.js中的httpServer来提供three.js服务

3

https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally建议使用本地服务器来提供three.js的示例。对我来说,Python SimpleHTTPServer很好用,但是我需要在three.js存储库克隆的examples目录上层运行它。

现在我正在尝试使用node.js中的httpServer提供相同的示例。我可以使用SimpleHTTPServer的node版本之一,但我需要一个httpServer对象通过socket.io将数据从服务器传递到webgl浏览器示例。因此,我采用了socket.io示例,并尝试使用以下server.jsexamples上层目录中运行它。

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);
  });
});

我在本地127.0.0.1:8080上使用node运行时无法看到three.js示例。即使我删除了所有对socket.io的引用,代码也无法工作,这表明问题出在html上。

  1. 我错过了什么?因为我没有得到回调错误,所以html文件被正确读取。

  2. 我注意到Python服务器将目录列为浏览器中的html链接。我点击examples来查看html文件,然后点击html文件,它可以正常工作。因此,我尝试在一个目录级别上运行'node server.js',使用几乎每个正反斜杠、根目录引用等组合,但都没有成功。

  3. 我不是纠结于纯httpServer。如果express或其他东西与socket.io和three.js一起工作,我会尝试它们。

1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接