Prevent executing next router if the request already handled

I am using connect middleware and have multiple router functions like below

var server = connect()
  .use(connect.cookieParser("secret"))
  .use(connect.session({secret: 'keyboard cat',cookie: { maxAge: 60000 }}))
  .use('/somepath', connect.static(__dirname + '/somepath'))
  .use('/_someapi', function (req, res) {
      // do something
  })
  .use(function (req, res) { // default case router
      // do something
  })
  .listen(8080)

My problem is, even if I request for /_someapi, after executing its function, the next router gets called. I want to know that how can I prevent it? I mean if the request is already handled, then the next router should not attempt to serve it.

Returning false from the function and res.end(''); doesn't help.

The problem here that it is possible that those are actually different requests triggering your middleware.

In order to check this: console.log(req.url). Most annoying issue is favicon.ico which gives lots of headache when you don't know that browser actually is requesting it all the time.

If you are developing RESTful API or any other similar application where favicon.ico is not required, then use this middleware as one of the first.

app.use(function(req, res, next) {
  if (req.url != '/favicon.ico') {
    return next();
  } else {
    res.status(200);
    res.header('Content-Type', 'image/x-icon');
    res.header('Cache-Control', 'max-age=4294880896');
    res.end();
  }
});

This will response with 200 and very long cache life so that way browser will not call it again anymore.

As well, just to clarify - are you sure you need to use .use instead of .get or any other routing solutions?