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