0

is there some simple example how to use cookies or sessions in towerjs? I read about Connect Middleware, but I have no experience with it (and unfortunately with node.js also).

If examples are not available I will be grateful for any tips too.

Marcin

Marcin Rogacki
  • 501
  • 6
  • 23

2 Answers2

1

Cookies

From the TowerJS source code here, the controller has these properties:

  • @request
  • @response
  • @cookies (which is just a shortcut for @request.cookies)
  • @session (which is just a shortcut for @request.session)

Hence to set cookies you can follow express documentation here

For example this set cookie 'rememberme'

# "Remember me" for 15 minutes 
@response.cookie 'rememberme', 'yes', 
  expires: new Date(Date.now() + 900000)
  httpOnly: true

And to get the cookie

@request.cookies.rememberme

Session

As for session, looks like it's just connect in memory session. See source code here https://github.com/viatropos/tower/blob/master/src/tower/server/application.coffee#L39

So you can just use it like:

@request.session.something = 'something'

or

@session.something = 'something'

Ok hope that helps...

250R
  • 35,945
  • 7
  • 33
  • 25
  • Yes, this what I needed. Just one thing. When I create session variable like in your example @session.something = 'something', I should have access to this variable in all subsites. But I do not have. In other subsite I get "undefined" output. Anyway thank you very much. Cookies works well :) – Marcin Rogacki May 15 '12 at 12:51
  • I'm not quite sure what you meant by 'subsite', but basically since it's default session store used by Tower is 'in-memory', so it's associated to a single node.js process. If you have multiple node.js processes, then you need to store the session externally, for example something like https://github.com/visionmedia/connect-redis – 250R May 15 '12 at 22:52
0

In the Tower.js github repo they are using sessions in the example here. Maybe you can get some help there. I'm not the coffee script guy, so my help is limited. ;)

But this is where they configure session/cookie support:

// config/application.coffee
@use "cookieParser", Tower.config.session.key
@use "session", secret: Tower.config.session.secret, cookie: {domain: Tower.config.session.cookie.domain}

I hope I could help at least a little! ;)

jsbeckr
  • 1,183
  • 1
  • 10
  • 24
  • thank you for your interest. Yes I am trying to use this configuration and I am trying right now implement a 'view counter' example from http://www.senchalabs.org/connect/session.html Unfortunately with no results. Well I am going to digging further. Greets! :) – Marcin Rogacki May 15 '12 at 09:29