Skip to content

Instantly share code, notes, and snippets.

@chrisveness
Last active August 29, 2015 14:15
Show Gist options
  • Save chrisveness/1e4eb603267ebbc32ca9 to your computer and use it in GitHub Desktop.
Save chrisveness/1e4eb603267ebbc32ca9 to your computer and use it in GitHub Desktop.
koa-flash fails when session domain opts set
var koa = require('koa');
var session = require('koa-session');
var flash = require('koa-flash');
var router = require('koa-router');
var handlebars = require("koa-handlebars");
var app = koa();
app.keys = ['foo'];
app.use(session({ domain: '.localhost' }, app)); // THIS FAILS
//app.use(session(app)); // THIS WORKS
app.use(flash());
app.use(handlebars({ extension: ['html'], viewsDir: './', partialsDir: './' }));
app.use(router(app));
var handler = {};
handler.get = function*() {
var context = {};
if (this.flash) context.flashmsg = this.flash.flashmsg;
console.log('handler.get flash', this.flash);
yield this.render('flash', context);
};
handler.post = function*() {
this.flash = { flashmsg: 'This is a flash' };
console.log('handler.post flash', this.flash);
this.redirect(this.url);
};
var routes = function(app) {
app.get('/flash', handler.get);
app.post('/flash', handler.post);
};
routes(app);
app.listen(process.env.PORT||3000);
console.log(process.version+' listening on port '+(process.env.PORT||3000)+' ('+app.env+')');
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flash test</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css">
</head>
<body>
<h1>Flash test</h1>
{{#if flashmsg}}<p>Flash message: {{flashmsg}}</p>{{/if}}
<form method="post">
<button type="submit">Submit</button>
</form>
</body>
</html>
{
"main": "app.js",
"scripts": {
"start": "nodemon --harmony -e js,json,html app.js"
},
"dependencies": {
"koa": "^0.17.0",
"koa-flash": "^0.1.0",
"koa-handlebars": "^0.5.0",
"koa-router": "^3.8.0",
"koa-session": "^3.1.0"
}
}
@chrisveness
Copy link
Author

Sorted! This was because cookies don’t work with ‘.localhost’ – see github.com/rickharrison/koa-flash/issues/5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment