Strange behaviour with koa-router and koa.session middlewares
See original GitHub issueIt looks like that if you use koa.session and koa-router middlewares you have to watch out for the order in which you install them.
Take a look at this example:
var app = require('koa')(),
session = require('koa.session'),
router = require('koa-router');
app.keys = ['secret'];
app.use(session())
app.use(router(app));
app.get('/test/print', function*(){
var v = this.session.value || 'Not FOUND';
this.body = 'You value is ' + v;
})
app.post('/test/:v', function*(){
this.session.value = this.params.v;
this.body = 'OK'
})
app.listen(8080);
console.log('Listening...')
If you execute this everything goes fine and you get back your value if you GET on the correct url.
If you switch the ``use` as follows:
app.use(router(app));
app.use(session())
and issue the exact command than before you get instead a
TypeError: Cannot read property 'value' of undefined
at Object.<anonymous> (index.js:14:22)
Where line 14:22 is around this.session.value
meaning that session is undefined.
I’ve created a makefile to reproduce the error and am posting this issue on the middleware’s repos as well.
Makefile:
POST_FILE=post
GET_FILE=get
COOKIE_JAR=biscotti
all:
@make example
@make show
#@make end
@killall node
clean:
rm -f ${POST_FILE}.txt ${POST_FILE}-debug.txt ${GET_FILE}.txt ${GET_FILE}-debug.txt ${COOKIE_JAR}
node:
DEBUG=koa node --harmony index.js&
@sleep 2
post:
@curl -s --cookie-jar ${COOKIE_JAR} --trace-ascii ${POST_FILE}-debug.txt -X POST http://127.0.0.1:8080/test/42 -o ${POST_FILE}.txt
get:
@sleep 2
@curl -s -b ${COOKIE_JAR} --trace-ascii ${GET_FILE}-debug.txt -X GET http://127.0.0.1:8080/test/print -o ${GET_FILE}.txt
@cat ${GET_FILE}.txt
@echo "\n"
redis:
redis-server&
@sleep 1
end:
@killall node
@echo 'shutdown nosave' | redis-cli --pipe&
example:
@make clean
#@make redis
@make node
@make post
@make get
show:
@echo "POST:\n"
@cat ${POST_FILE}.txt
@echo "\n\n"
@echo "GET:\n"
@cat ${GET_FILE}.txt
@echo "\n"
.PHONY = example post get node redis end show all
Issue Analytics
- State:
- Created 10 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Need a little help understanding Node.js behavior. Why does ...
So I am just to trying to write a hello-world server with koa and koa-router. This is my code. const Koa = require(' ......
Read more >Koa.js - Quick Guide - Tutorialspoint
Koa is an object, which contains an array of middleware generator functions, all of which are composed and executed in a stack-like manner...
Read more >koa-router middleware not working on route level - Reddit
When there are no more middleware functions, Koa works its way back down the stack. Koa pops the topmost middleware off the stack...
Read more >Let's build a REST API with Koa.js and test with Jest! - codeburst
Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and ...
Read more >How to use the koa-generic-session function in koa ... - Snyk
To help you get started, we've selected a few koa-generic-session examples, ... .use(passport.session()) .use(compress()) .use(router.routes()) .use(router.
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
Isn’t this what would be expected? I believe you can think of the middlewares as a stack like this:
while reordering would make it something like this:
so if it’s the other way around the router is run (and thus the routes?) before the session middleware which makes
this.session
undefined.But please correct me if I misunderstood the issue.
And thank you for reporting it 👍