question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

koa session does not work when used in a middleware app

See original GitHub issue

I 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:open
  • Created 9 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisvenesscommented, Mar 23, 2018

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 on localhost can be problematic.

1reaction
chrisvenesscommented, Jan 19, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found