0

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

Yassine
  • 5
  • 1
  • 3
  • Some points to be clear about that might help you. `send` is part of express module. It's a higher level method, meaning it takes care of stuff for you, like you don't have to worry about if you are sending JSON, a number, or just text. Also, `res.render()` is the method that takes your content, and passes it to Jade (expresses default templating library) before it gets sent back to client. – cathy.sasaki May 29 '13 at 16:49
  • And check out this question: http://stackoverflow.com/questions/4529586/render-basic-html-view-in-node-js-express – cathy.sasaki May 29 '13 at 16:51

2 Answers2

1

You can't redirect to index.jade from client side. 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.

// 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; 
});
lyxio
  • 123
  • 2
  • 9
  • please can u put me an example and tell me where can i use it in my code server, because i want redirect after recovering username and password values – Yassine May 29 '13 at 18:27
0

There is several problems in your application:

  1. send is not a standard method of node.js
  2. When sending file to client, there is no need to read file as utf8 text, just read it as buffer
  3. There is a syntax error while running script below from inside a browser.

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>
kyriosli
  • 333
  • 1
  • 6
  • I tried but it does not work apparently I can't do it in client :/ and i don't know how to do it in server – Yassine May 29 '13 at 18:29
  • First of all, IT IS possible to redirect to another address by client-side script; Then, TI IS impossible to send a redirect to the client while the client is not requesting a new page. – kyriosli May 30 '13 at 03:59