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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With