Skip to content

Instantly share code, notes, and snippets.

@shimaore
Last active November 16, 2015 12:04
Show Gist options
  • Save shimaore/7959885429c4ecc5de92 to your computer and use it in GitHub Desktop.
Save shimaore/7959885429c4ecc5de92 to your computer and use it in GitHub Desktop.
Test Express behavior wrt next() / next('route') / ..
var express = require('express')
var app = express()
var handler = function (req,res,next,where,what) {
console.log(' ' + where + ' === ' + what)
switch(what) {
case 'stop':
console.log(' ' + where + ':stop -> (do nothing)')
break
case 'next':
console.log(' ' + where + ':next -> next()')
next()
break
case 'text':
console.log(' ' + where + ":text -> next('" + where + ":text')")
next('' + where + ':text')
break
case 'error':
console.log(' ' + where + ":error -> next(new Error('" + where + ":error'))")
next(new Error('' + where + ':error'))
break
case 'route':
console.log(' ' + where + ":route -> next('route')")
next('route')
break
case 'send':
console.log(' ' + where + "send -> res.send('" + where + "-send')")
res.send('' + where + '-send')
break
case 'send-and-next':
console.log(' ' + where + "send-and-next -> res.send('" + where + "-send-and-next'); next()")
res.send('' + where + '-send-and-next')
next()
break
case 'auth':
switch( req.query.user ) {
case 'next':
console.log(' ' + where + ":auth(next) -> next()")
next()
break
case 'route':
console.log(' ' + where + ":auth(route) -> next('route')")
next('route')
break
default:
next(new Error('' + where + ':auth(*), invalid user ' + req.query.user))
break
}
break
case 'auth-route':
if(req.query.user === 'admin') {
console.log(' ' + where + ":admin -> next('route')")
next('route')
} else {
next(new Error('' + where + ': invalid user: ' + req.query.user))
}
break
default:
console.log(' ' + where + ":* -> res.send('" + where + "-default')")
res.send('' + where + 'default')
break
}
}
app.use(function(req,res,next) {
console.log('')
next()
})
app.use(function(req,res,next) {
console.log('USE')
if( req.query.use ) {
handler(req,res,next,'use',req.query.use)
} else {
next()
}
})
app.use(function(req,res,next) {
console.log('USE2')
if( req.query.use2 ) {
handler(req,res,next,'use2',req.query.use2)
} else {
next()
}
})
app.get('/base/:head/:tail', function (req,res,next) {
console.log('GET /:head/:tail -- head === ' + req.params.head)
handler(req,res,next,'head',req.params.head)
}, function (req,res,next) {
console.log('GET /:head/:tail -- tail === ' + req.params.tail)
handler(req,res,next,'tail',req.params.tail)
})
app.get('/all/:head/:tail/*', function (req,res,next) {
console.log('GET /:head/:tail -- head === ' + req.params.head)
handler(req,res,next,'head',req.params.head)
}, function (req,res,next) {
console.log('GET /:head/:tail -- tail === ' + req.params.tail)
handler(req,res,next,'tail',req.params.tail)
})
app.get( /./, function(req,res,next) {
console.log('GET ' + req.path + ' -> res.send("' + req.path + ' OK")')
res.send('' + req.path + ' OK')
})
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
})
@shimaore
Copy link
Author

Compare for example:

http://127.0.0.1:3000/base/route/send # send is skipped
http://127.0.0.1:3000/base/next/route # next('route') behaves same as next() in last callback
http://127.0.0.1:3000/base/next/route?use=route # next('route') behaves as res.send('route') in middleware

http://127.0.0.1:3000/base/auth/send/foo # No auth is performed
http://127.0.0.1:3000/all/auth/send/foo # Auth rejected
http://127.0.0.1:3000/all/auth/send/foo?user=bar # Auth rejected
http://127.0.0.1:3000/all/auth/send/foo?user=next # Auth accepted, res.send() executed, no next()
http://127.0.0.1:3000/all/auth/send-and-next/foo?user=next # Auth accepted, res.send() executed, complains about doing res.send() twice

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