course

A framework-independent express-like router

course

A framework-independent express-like router.

Creates a new router.

Adds a new middleware to the router. method is an HTTP verb, such as get, post or put, to filter requests by the request method used.

The first argument may optionally be a URL pattern or regular expression to match URLs against. Each argument after the URL is an express-style middleware function with the signature (req, res, next).

Same as above, however without filtering by request method.

var db     = require('some-database')
var course = require('course')
var http   = require('http')
var router = course()
 
var server = http.createServer(function(reqres) {
  router(req, res, function(err) {
    if (err) return res.end(err.message)
    res.end('404')
  })
})
 
router.all(function(reqresnext) {
  // do something on every request... 
  next()
})
 
// pass in URL filters, and multiple 
// middlewares 
router.get('/'
  , getUserData
  , loadHomeView
  , render
)
 
function getUserData(reqresnext) {
  db.getUser(function(erruser) {
    if (err) return next(err)
    req.user = user
    next()
  })
}
 
function loadHomeView(reqresnext) {
  req.view = require('./views/home')
  next()
}
 
function render(reqresnext) {
  req.end(req.view({
    user: req.user
  }))
}
 
// Or capture URL paths: 
router.get('/user/:username/*', function(reqresnext) {
  var name = this.username
  var page = this[1]
})

MIT. See LICENSE.md for details.