Norman Bird
Norman Bird

Reputation: 682

how to access session variables from express and nodes js in function.js

I am able to set and access session variables, like req.session.t1, etc. and all works fine on server side. On client side I have jquery installed on the front end and accessed by jade template.

I have in functions.js:

$(function(){          

    var t1 = req.session.t1;      

    $('.results').append(t1); 

});         

So as page/jade template loads, jquery does append the the data as outlined IF I use constants or a string, but session variables are undefined if I use req.session.x or session.x.

So question is, how would I best be able to manipulate via jQuery, session variables? Or do I need to save to DB and pull it up each time? I'm newbie and not sure how to do this in node js and express and jquery.

Upvotes: 0

Views: 10328

Answers (3)

jfriend00
jfriend00

Reputation: 707716

I'm hoping that you understand that your session state is on the server and your jQuery is in the browser and those are completely different computers. There is no req object in the web page on the browser. That is a server-side object that existed only while the web request was being processed and no longer exists once the web page has been sent to the browser and is being processed by the browser.

So, that explains why there is no req object in the browser.

There are several ways you could make some information from the session available in the browser.

  1. You can use your template system to generate some Javascript variables in your web page that contain the desired information. Then, your jQuery could read the value of those Javascript variables in your web page and do what it wants with them.

  2. You could create an ajax call that you could make from your jQuery in the browser to contact the server and request information from the user's session.

  3. You could set some specific session information (like a username) into a cookie by the server and then that cookie would be readable from your jQuery code in the client.

Upvotes: 1

Norman Bird
Norman Bird

Reputation: 682

I found the answer at this answer

I altered the best answer to suit my case and it was

script(type='text/javascript').
    var t1 = #{session.t1}

Turns out that Jade templates can use session variables from express to create the page, but once created and displayed, they are no longer available to front end JavaScript calls, BUT if you specifically use them to create javascript variables, then those ARE available to front end javascript, and thats how you pass it along to your jquery and javascript. cool huh?

Upvotes: -2

Ven
Ven

Reputation: 19040

Node.js/express run on the server.

jQuery runs on the client.

The only thing that's shared is whatever you've sent to the client.

If you want a variable to be accessible client-side, you need to send it in the response.

If you're giving the server JSON back, just add a field.

res.send({t1: req.session.t1}); // (node.js side)

If you're outputting a template, you can write it in a HTML tag, or directly in a script tag.

Jade example:

script. // .jade file
  var t1 = "#{t1}"

And in the server:

res.render('template.jade', {t1: req.session.t1});

Upvotes: 2

Related Questions