Last active
November 16, 2015 12:04
-
-
Save shimaore/7959885429c4ecc5de92 to your computer and use it in GitHub Desktop.
Test Express behavior wrt next() / next('route') / ..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compare for example: