简体   繁体   English

使用JavaScript重定向到一个玉文件

[英]redirect to a jade file using javascript

I would like to redirect to a file index.jade using javascript but it does not work with document.location.href = "index.jade", so how can i do ? 我想使用javascript重定向到文件index.jade,但不能与document.location.href =“ index.jade”一起使用,那我该怎么办? this is what I did : 这就是我所做的:

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');

        });   
    });

CLIENT: 客户:

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

thx for all. 谢谢大家。

You can't redirect to index.jade from client side. 您不能从客户端重定向到index.jade。 You need to redirect to a location which is handled on by the server and in response, render the index.jade file and send it to the client. 您需要重定向到服务器处理的位置,并作为响应,呈现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; 
});

There is several problems in your application: 您的应用程序中存在几个问题:

  1. send is not a standard method of node.js send不是node.js的标准方法
  2. When sending file to client, there is no need to read file as utf8 text, just read it as buffer 将文件发送到客户端时,无需读取文件为utf8文本,只需将其读取为缓冲区
  3. There is a syntax error while running script below from inside a browser. 从浏览器内部运行下面的脚本时出现syntax error

So just try this: 因此,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM