koa session does not work when used in a middleware app
See original GitHub issueI am using the vhost middleware with koa-session. Since koa-session 2.0.0 is was possible to use it in a subapp. Since 3.0.0 this does not work anymore. Example:
var session = require('./');
var koa = require('koa');
var compose = require('koa-compose');
var app = koa();
app.keys = ['some secret hurr'];
app.use(session(app));
app.use(function* (next){
if ('/favicon.ico' == this.path) return;
var n = this.session.views || 0;
this.session.views = ++n;
this.body = n + ' views';
});
var server = koa();
server.use(compose(app.middleware));
server.listen(3000);
console.log('listening on port 3000');
Issue Analytics
- State:
- Created 9 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top Results From Across the Web
How do I fix this TypeScript issue with Koa Session using a ...
An ugly workaround is retyping the app in the call like app.use(session(app as unknown as Koa)) but I really hope there is a...
Read more >koa-session - npm
Simple session middleware for Koa. Defaults to cookie-based sessions and supports external stores. Requires Node 7.6 or greater for async/await ...
Read more >Top 5 koa-csrf Code Examples - Snyk
To help you get started, we've selected a few koa-csrf examples, based on popular ways it is used in public projects. Secure your...
Read more >Koa.js - Sessions
Koa.js - Sessions, HTTP is stateless, hence in order to associate a request to ... app.use(session(app)); // Include the session middleware app.use(function ......
Read more >oak | A middleware framework for handling HTTP with Deno ...
This middleware framework is inspired by Koa and middleware router inspired by ... To get an application to use a middleware function, an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t recall any longer what the issue turned out to be, but I am successfully using koa-session on a per-app basis with Koa v2: e.g. https://github.com/chrisveness/koa-sample-web-app-api-mysql/blob/master/app.js (don’t know about a ‘recommended’ solution!).
Things are a bit more complex if you want to have session variables common to multiple sub-apps: you would have to use the
domain
option, setting it to the super-domain common to your sub-domains. Bear in mind, though, that for testing, cookies onlocalhost
can be problematic.I am also having problems with koa-session (in fact, koa-flash) which I suspect may be related to this.
I am using virtual host apps as per https://github.com/koajs/examples/tree/master/vhost.
Is it possible to use koa-session with this configuration? If so, could you give me a pointer as to how it should be done? If not, have you any other suggestions?
Thanks.