Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express: passing parameters between client pages

I have a Node.js server. Lets say each client has his name saved on a variable. They switch page and I want each client to mantain their name on a variable.

This would be very easy with a php form, but I can't see how to do it with Node.js If I do a form like I would do in php, I manage to send the name to the server:

app.post('/game.html', function(req, res){
    var user = req.param('name');
    console.log(user);
    res.redirect('/game.html');
    });

But it seems too complicated to then resend it again to each client it's own. I just started with Node.js, I guess it's a concept error. Is there any easy way to pass a variable from one page in the client to another? Thanks.

like image 343
Kal Avatar asked Feb 12 '13 09:02

Kal


1 Answers

Instead of redirecting to a static file, you have to render the template ( using any engine that ExpressJS supports ):

app.post('/game.html', function(req, res){
    var user = req.param('name');
    console.log(user);
    res.render( 'game.html', { user:user } );
});

( note that .render requires some additonal settings set on app )

Now user variable becomes available in game.html template.

like image 157
freakish Avatar answered Nov 15 '22 08:11

freakish