如何将当前的静态html文件更改为Nodejs中的另一个html文件?

我想让我的nodejs脚本来改变显示某个socket.io时显示的HTML文件看到我的脚本下面

var app = require('express')(); var server = require('http').createServer(app); var bin = "casperjs"; var args = ['server.js']; var io = require('socket.io').listen(server); server.listen(process.env.PORT); app.get('/', function (req, res) { res.sendfile(__dirname + '/client/index.html'); }); io.sockets.on('connection', function (socket) { console.log("socket connection incoming"); socket.on('dout', function () { var spawn = require('child_process').spawn, child = spawn(bin, args); child.stdin.setEncoding = 'utf-8'; socket.on('trout', function(data){ child.stdin.write(data.message + '\n'); //child.stdout.pipe(process.stdout); child.stdout.on('data', function (data) { console.log(' ' + data); }); socket.on('mout', function() { console.log('communicatio\n'); /* trying to change the html file displayed to account.html from index.html i have tried this with no success app.get('/', function (req, res) { res.sendfile(__dirname + '/client/index.html'); }); */ }); }); // console.log(child.stdout); //console.log(child.stdout.pipe(write.stdout)); }); }); 

这是我正在努力去工作

 socket.on('mout', function() { console.log('communicatio\n'); /* trying to change the html file displayed to account.html from index.html i have tried this with no success app.get('/', function (req, res) { res.sendfile(__dirname + '/client/index.html'); }); */ 

任何人都可以帮助我如何正确地做到这一点

Socket.io事件发生在快速应用程序的传统请求/响应生命周期之外。 一旦用户加载了你的初始路线,一条快速路线只会被另一个请求触发。 您应该通过socket.io处理通信并emit方法。

像下面的psuedocode应该工作:

服务器

 socket.on('mout', function() { // this may be a little heavy handed // ideally you are only updating small parts of the page as they change // or notifying the client that they need to make an additional request ajaxically fs.readFile('__dirname + '/client/index.html', 'utf8', function (err, content) { if (err) return; socket.emit('pageUpdate', function () { content: content }); }); }); 

客户:

 // when the pageUpdate event is fired, // change the body content of the response socket.on('pageUpdate', function (content) { $('body').html($('body', content)); });