Created
January 26, 2012 06:13
-
-
Save gigonaut/1681336 to your computer and use it in GitHub Desktop.
simple director with next routing. see the array of routes in the router.
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
// | |
// require the native http module, as well as director. | |
// | |
var http = require('http'), | |
director = require('director'); | |
// | |
// create some logic to be routed to. | |
// | |
function pre(next) { | |
setTimeout(function() { | |
console.log('pre works'); | |
next(); | |
}, 2000); | |
} | |
function helloWorld() { | |
console.log('helloworld works') | |
this.res.writeHead(200, { 'Content-Type': 'text/plain' }) | |
this.res.end('hello world'); | |
} | |
// | |
// define a routing table. | |
// | |
var router = new director.http.Router({ | |
'/hello': { | |
get: [pre, helloWorld] | |
} | |
}).configure({async: true}); | |
// | |
// setup a server and when there is a request, dispatch the | |
// route that was requestd in the request object. | |
// | |
var server = http.createServer(function (req, res) { | |
router.dispatch(req, res, function (err) { | |
if (err) { | |
res.writeHead(404); | |
res.end(); | |
} | |
}); | |
}); | |
// | |
// You can also do ad-hoc routing, similar to `journey` or `express`. | |
// This can be done with a string or a regexp. | |
// | |
router.get('/bonjour', helloWorld); | |
router.get(/hola/, helloWorld); | |
// | |
// set the server to listen on port `8080`. | |
// | |
server.listen(9191); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment