简体   繁体   English

如何在Node.js中将当前的静态html文件更改为另一个html文件?

[英]How to change the current static html file to another html file in Nodejs ?

i am trying to get my nodejs script to change the html file displayed when a certain socket.io is fired see my script below 我正在尝试让我的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));
    });
});

this is what i am trying to get to work 这就是我想要去上班的

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


       */

can anyone help me on how i can do this correctly 谁能帮助我正确地做到这一点

Socket.io events happen outside of the traditional request/response lifecycle of an express app. Socket.io事件发生在Express应用程序的传统请求/响应生命周期之外。 Once the user has loaded your initial route, an express route will only get triggered by an additional request. 一旦用户加载了您的初始路线,快速路线将仅由其他请求触发。 You should handle communication via socket.io's on and emit methods. 您应该通过socket.io的onemit方法。

Something like the following psuedocode should work: 类似于以下psuedocode的东西应该可以工作:

Server 服务器

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

});

Client: 客户:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM