buydadip
buydadip

Reputation: 9437

How to change value of html header

I am using redirect to pass the current user's username to some other page, as so...

var current_user = encodeURIComponent(req.body.log_username);
res.redirect('/home?valid=' + current_user);

...The receiving page has the following code...

router.get('/', function (req, res) {
    res.render('home', { title: 'Express' });
    var passedVariable = req.query.valid;
    console.log(passedVariable);
});

The correct username is passed through, but I need to know how I can incorporate the username into my html.

For example, when the html is rendered, I want the header to say "Welcome 'username'"

How can I do this? With php this is relatively simple, but I am at a loss with node js.

Upvotes: 1

Views: 49

Answers (1)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22895

You will send a post request to login and after successful login, you will be redirected to / route. Here you have username in passedVariable variable in your code. You can pass this variable to main page where you can render it in your header.

router.get('/', function (req, res) {
   var passedVariable = req.query.valid;
   console.log(passedVariable);
   res.render('home', { title: 'Express', username: passedVariable });
});

Now you will get this variable in your main page template and you can use it. For ejs, code will look like

<p><%= username %></p>

For jade

p= username

Templates side code depends on your choice of technology, whether you are using ejs or jade

Upvotes: 2

Related Questions