2017-07-27 33 views
0

我正在嘗試使用套接字io和節點來顯示兩個htmls。我有:點擊按鈕後,我如何重定向到不同的頁面?

app.get("/", function(req, res) { 
    res.sendFile(__dirname + "/login.html") 
}) 

用戶登錄並點擊登錄按鈕。這會將信號發送回服務器。然後,我希望服務器發送一個不同的HTML頁面給用戶。 在login.html的,我有:

socket.emit('username', $('#username').val()); 

這是由服務器接收:

socket.on("username", function(username){ 
      res.redirect(__dirname + "/index.html"); 
    }) 

但是,我不能使用資源,因爲它的空,頭已經從SENDFILE設置當用戶到達login.html頁面時。我應該如何將index.html(在服務器上)發回給用戶?

回答

1

您可以發出從服務器端「重定向」事件,聽它在客戶端,並且對客戶端重定向邏輯

// server side pseudo code 
socket.on("username", function(username){ 
     client.emit("redirect"); 
    }) 

// client side 

server.on('redirect', function(){ 
    //redirect here 
    window.location.href = /url here/ 
}) 
相關問題