如何导航到另一个HTML页面?

时间:2013-10-20 15:43:09

标签: javascript html 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('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){
;
});
 });

有人可以建议我该怎么办?

2 个答案:

答案 0 :(得分:1)

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

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

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.

答案 1 :(得分:-2)

喜欢这个

window.location.replace("http://www.website.com/student.html");