1

在我的应用程序中,有一个通常的登录页面将用户名和密码发送到服务器脚本,需要对其进行身份验证,如果是真实用户,服务器应该重定向到页面 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('check',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: '*******',
        database: 'my_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','DB error');
            return;
        }
        console.log('The solution is: ', rows[0].solution);
        if (rows>0) 
                    //***** Here i want redirection to another page ****** 
        else socket.emit('login_failure','Invalid Username or password');
    });

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

 iot.sockets.on('connection', function(socket){
;
});
 });

谁能建议我该怎么做?

4

2 回答 2

1

一种方法是在响应中发送重定向标头。添加以下内容代替您的评论。

response.setHeader('Location','your_redirection_url');

Read : http://nodejs.org/docs/v0.4.12/api/http.html#response.setHeader

One other way is to use meta redirect. print this as part of the <head> section. This will redirect the page automatically.

<meta http-equiv="Refresh" content="0; url=http://your_redirection_url" />

于 2013-10-20T15:51:59.403 回答
-2

像这样

window.location.replace("http://www.website.com/student.html");
于 2013-10-20T15:48:05.097 回答