Simplest regex router for connect (56 lines of code) with the support of defining route specific logic.

npm install flat-route
1 downloads in the last week
4 downloads in the last month


Simplest router for connect (56 lines of code) which supports route specific logic. See usage below.


npm install flat-route


// Controller, action.
var users = {
  create: function(req, res, next) {
    // Do model job.

// Routes.
var routes = [
  ['POST', /^\/users$/, users.create, {
    auth: true

var connect = require('connect');
var flatRoute = require('flat-route')(routes);

var app = connect()

  // First we attach (at this point, flatRoute sets the req.route)

  // No we can create our custom route logic ... (input validation, auth logic, etc.)
  // Little example below:
  .use(function(req, res, next) {
    if (req.route.data.auth) {
      // Check for session auth?


  // Finally we apply, which calls the callback of the route (3th array element of a route).



By default, if no matching route was found, flatRoute will throw a 404 and end the request, however you can overwrite this logic. See the example below:

var flatRoute = require('flat-route')(routes, {
  notFound: function(req, res, next) {
    // Do my custom 404 logic here.

// ... continue as connect middleware (see Usage)
npm loves you