Can a Director route handler abort or return an error

44 views
Skip to first unread message

Stuart Malin

unread,
Apr 30, 2012, 12:27:50 PM4/30/12
to flati...@googlegroups.com
Is it possible for a route handler to abort (that is, not generate any response)?

I'd like to be able to have certain route handlers abort in such a way that either 
(a) the router treats the request as unhandled, or 
(b) an error object is returned to the middleware.

Charlie Robbins

unread,
Apr 30, 2012, 10:27:42 PM4/30/12
to flati...@googlegroups.com
Stuart,

It is very possible to do this:

var router = new director.http.Router();

//
// Router treats the request as unhandled
//
router.get('/', function (next) {
  next();
});

//
// Router responds with an error to the next middleware
//
router.get('/', function (next) {
  next(new Error('OHAI I'm an error');
});

It's worth noting that the `next` callback always comes after any capture groups in the routing table:

e.g.

//
// Setup a router path with a regexp and capture group
//
router.path(/\/(\w+)/, function () {
  //
  // Then setup a second regexp and capture group
  //
  router.get(\/(\w+)/, function (outerCapture, innerCapture, next) {
    //
    // ***Notice there are two parameters before next***
    //
  });
});

Cheers,
Charlie

Stuart Malin

unread,
May 1, 2012, 10:46:44 AM5/1/12
to flati...@googlegroups.com
Charlie,

Of course I had tried that... but for my code, no function is passed to the route handler.

Test code:
//------------------------
router = new director.http.Router();

router.get('/', function (next) {
  var res = this.res;
  res.writeHead(200, {"Content-Type": "text/plain"});
  res.end("Hello World! next=" + next);
});
//------------------------

displays next as "undefined'

NOTE: 'next' is defined as a function if I configure for aync:

router = new director.http.Router().configure({
  async: true
});

Should that matter?

I am using Director 1.0.10
node 0.6.14

TIA,
~Stuart~
Reply all
Reply to author
Forward
0 new messages