0

I have two pages, login and myservices. After a user logs in, they will be brought to myservices page, and their username should be shown.

in login.js, I have:

req.session.user = "abc";
res.redirect('/myservices');

and in myservices.js, I have:

router.get('/', function(req, res, next) {
  console.error("went through get in my services");
  console.error(req.session.user);
  res.render('myservices', {
    user: req.session.user
  });
});

in my template (myservices.ejs), I try to print out user:

<%if(user){%>
  <%=<p>user</p>%>
<%}%>

Whenever I login, I get redirected to myservices page and the console shows

went through get in my services
abc

but in the browser, it does not print out "abc" until I refresh the page. Why is not rendered to myservices.ejs on the first time but on the second time?

Any help is greatly appreciated!

Louis.L
  • 13
  • 1
  • 4

1 Answers1

0

You haven't passed the local yet the first time. Render in login.js instead of redirecting and pass the local.

req.session.user = "abc";
res.render('myservices', {
    user: req.session.user
});
  • If I render from login.js to myservices, my url will end up being /login. I want my url to be /myservices, is there any ways to achieve that? – Louis.L Jun 30 '16 at 16:50