0

我是 node.js 的新手,我正在开发一个简单的应用程序,实时数据分布将在学生和老师之间发生。学生首次登录并在服务器验证后将被重定向到学生界面。
我已完成所有身份验证和数据分发工作,请帮助我如何重定向到 student.html

var ports = 3000;
var portt = 3001;
var express = require('express');
var student = require('express')();
var teacher = require('express')();
var server_s = require('http').createServer(student);
var server_t = require('http').createServer(teacher);
var ios = require('socket.io').listen(server_s);
var iot = require('socket.io').listen(server_t);
var path = require('path');

server_s.listen(ports);
server_t.listen(portt);

student.use(express.static(path.join(__dirname, 'public')));
student.get('/', function(req,res){
    res.sendfile(__dirname + '/login.html');
});

teacher.use(express.static(path.join(__dirname, 'public')));

teacher.get('/', function(req,res){
        res.sendfile(__dirname + '/mytry.html');
});


ios.sockets.on('connection', function(socket){
     var username, password;



socket.on('verify',function(data){
username = data[0];
password = data[1];
//************* Database connection and query *************
var mysql      = require('mysql');
var connection = mysql.createConnection({
host     : 'localhost',
user     : 'user',
password: 'pswd',
database: 'db'
});

connection.connect();
var qstring = 'SELECT s_id FROM login_student WHERE username='+username+'AND password='+password;
connection.query(qstring, function(err, rows, fields) 
{
    if (err) 
    {   
        console.log('ERROR: ' + err);
            socket.emit('login_failure','database error');
        return;
    }
    console.log('The solution is: ', rows[0].solution);
    if (rows>0) alert('Login Successfull');///// **I want redirection here** 
    else socket.emit('login_failure', 'invalid user name or password');
});

connection.end();
});
});

iot.sockets.on('connection', function(socket){
    socket.on('sendlesson', function(lesson){
    ios.sockets.emit('updatelesson', lesson);
});
});
4

1 回答 1

1

socket.io没有“重定向”的概念,因此您将发出一条消息,指示您的客户端代码重定向:

// in your socket.io code
if (rows > 0)
  socket.emit('login_success');
else
  socket.emit('login_failure', 'invalid user name or password');

// in your client-side code
socket.on('login_success', function() {
  location.href = '/student.html';
});

(但这显然不会阻止任何人/student.html直接访问)

于 2013-10-20T13:06:19.603 回答