使用javascript重定向到jade文件

时间:2013-05-29 16:10:55

标签: javascript node.js sockets

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

SERVER:

 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>
总而言之。

2 个答案:

答案 0 :(得分: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; 
});

答案 1 :(得分:0)

您的申请中存在以下几个问题:

  1. send不是node.js的标准方法
  2. 将文件发送到客户端时,无需将文件读取为utf8文本,只需将其作为缓冲区读取
  3. 在浏览器内部运行脚本时有一个syntax error
  4. 所以试试这个:

    <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>