How can I set and read session data in RingoJS?

320 views Asked by At

Good day,

I have recently started playing with RingoJS along with Stick middleware and I am now stuck with sessions. I can't make it work.

main.js

var {Application} = require( "./_lib_/stick" ),
    app = exports.app = Application();

app.configure( "mount" );
app.mount( "/", require( "./actions" ) );

if ( require.main === module ) {
    require( "ringo/httpserver" ).main( module.directory );
}

actions.js

var {Application, helpers} = require( "./_lib_/stick" ),
    Response = require( "ringo/jsgi/response" );

export( "app" );

var app = Application();

app.configure( "route", "render", "session" );
app.render.base = module.resolve( "templates" );
app.render.master = "page.html";

app.get("/session.htm", function( request ) {
    //request.session.data.foo = "bar";
    request.session.data.put("foo", "bar");

    return Response.redirect( "session2.htm" );
});

app.get("/session2.htm", function( request ) {
    var value = request.session;

    return Response.html( "Session: " + value );
});

I've tested many combinaison but none of them worked as espected.

Most errors I get is about reading the data from the session. For example:

TypeError: Cannot read property "data" from undefined

Any tips?

1

There are 1 answers

2
Matthew Crumley On BEST ANSWER

"session" needs to come before "mount" in app.configure.

On the fourth line of main.js:

app.configure( "session", "mount" );

Then, remove "session" from the later call to app.configure since there's no point in running the middleware twice.