0

我想使用javascript重定向到文件index.jade,但它不适用于document.location.href =“index.jade”,我该怎么办?这就是我所做的:

服务器:

 app.get('/', function(req, res) {
        fs.readFile(__dirname + '/login.html', 'utf8', function(err, text){
            res.send(text);
        }); });    
    var io = require("socket.io").listen(server); 
    io.sockets.on('connection', function (socket) {   
    socket.on('login',function(data){
            console.log(data);
            socket.emit('redirect_to_chat1');

        });   
    });

客户:

<script>
$(document).ready(function() {
var socket = io.connect('http://localhost:8080');
$('button#bd1').on({ click:function(e){
socket.emit('login',{username:$('#login').val(),password:$('#pass').val()});
socket.on('redirect_to_chat1', function(){
    document.location.href="index.jade";
});
});

</script>

谢谢大家。

4

2 回答 2

1

您不能从客户端重定向到 index.jade。您需要重定向到由服务器处理的位置,作为响应,呈现 index.jade 文件并将其发送到客户端。

// First, create an express server..
var express = require('express');
var app = express();

// Configure your server, make sure to put the correct paths
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
// You'll need express.session to manage logged in users.
app.use(express.session({
    secret: "TOP SecerT",
    store: new require('connect')()
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// Now manage your routes.. 
// Instead of opening login.html using fs, just put your main content to the index.jade 
// and render it as follows...

// For all the requests, if the user is not logged in, redirect to '/login'
app.use(function(req, res, next) {
    if (! req.session.loggedIn) 
        return res.redirect('/login');
    next();
});
app.use('/', function(req, res, next){
    res.render('index.jade');
});

// Now, handle login requests here..
app.get('/login', function(req, res){
    // Assuming that you put your login code into a template called login.jade
    res.render('login.jade');

    // OR if you don't have a login template just use res.sendfile('login.html');
});

// In your client side, you need to post the login credentials to the server, 
// and here's the handler for that.
app.post('/login', function(req, res){
    // Check the user credentials. This is normally getting done by querying a db, but 
    // let's make it simple here.
    if (req.param('username') == 'foo' && req.param('password') == 'bar') 
        req.session.loggedIn = true;
});

// A simple handler for logout requests..
app.get('/logout', function(req, res){
    // This will delete all your session data...
    req.session = null; 
});
于 2013-05-29T16:40:10.523 回答
0

您的应用程序中有几个问题:

  1. send不是node.js的标准方法
  2. 向客户端发送文件时,无需将文件读取为 utf8 文本,只需将其读取为缓冲区即可
  3. syntax error从浏览器内部运行下面的脚本有一段时间。

所以试试这个:

<script>
$(function() {
    var socket = io.connect('http://localhost:8080');
    $('button#bd1').on({ click:function(e){
        socket.emit('login',{username:$('#login').val(),password:$('#pass').val()});
        socket.on('redirect_to_chat1', function(){
            location.href="index.jade";
        });
   }});
});
</script>
于 2013-05-29T16:39:37.820 回答