Skip to content

Instantly share code, notes, and snippets.

@DTrejo
Created February 9, 2012 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DTrejo/1783626 to your computer and use it in GitHub Desktop.
Save DTrejo/1783626 to your computer and use it in GitHub Desktop.
notfound not called
var http = require('http')
, director = require('director')
, PORT = process.env.port || 8000
var routes = {
'/hello': {
get: function(route) { this.res.end('/hello') }
}
}
var routerOptions = { notfound: notFound }
var router = new director.http.Router(routes).configure(routerOptions)
var server = http.createServer(function(req, res) {
router.dispatch(req, res)
})
// strangely, this does not work:
// http.createServer(router.dispatch)
// called when no route matches, like for static files!
function notFound(req, res) {
console.log('notFound entered!');
req.end('yay, notFound was called!')
}
server.listen(PORT)
console.log('Listening on http://localhost:'+PORT)
@DTrejo
Copy link
Author

DTrejo commented Feb 9, 2012

solution:

var server = http.createServer(function (req, res) {
  router.dispatch(req, res, function(err) {
    if (err) console.log(err);
    notFound(req, res)
  })
})

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