Node.js Express: passing parameters between client pages

I have a Node.js server. Let's say each client has its own name stored in a variable. They switch the page and I want each client to pass their variable name.

It would be very easy with a php form, but I can't see how to do it with Node.js If I make a form like in php I manage to submit 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 re-send it again to every client that owns it. I am just starting out with Node.js, I guess this is a concept error. Is there an easy way to pass a variable from one page to another client? Thank.

+3


source to share


2 answers


Instead of redirecting to a static file, you should render the template (using any engine supported by ExpressJS):

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 additional tweaks set to app

)

Now the variable user

becomes available in the template game.html

.

+7


source


You can use res.render and pass many variables like:



res.render('yourPage', {username:username, age:age, phone:phone});

      

0


source







All Articles