copperhead

Connect compatible router middleware that supports content negotiation

Copperhead

Copperhead is a connect compatible router middleware that supports content negotiation.

npm install copperhead

The router object has a method for each HTTP method. Each method needs at least a handler function that gets req und res as parameters. If the function returns nothing, the next middleware will be used. If the function returns a string or object it will be used as response with an appropriate content type.

Optionally a method takes a path and a content type. The path variables are exposed to the this object of the handler.

router.method [path], [content type], (req, res) ->

So for example if we want to use the GET method at the path /users/:name for the content type text/html we would do

router.get '/users/:name''text/html'(req, res) ->
    'Hello user '+@name+'!'

A more complete example

connect = require 'connect'
 
router = require 'copperhead'
 
app = connect()
app.use connect.logger 'dev'
app.use router
 
router.get '/foo'->
    'bar!'
 
router.get ->
    'Hello World!'
 
router.get 'application/json'->
    hello: "World!"
 
router.get 'text/html'->
    '<!DOCTYPE html><html><head><title>Hello World!</title></head><body></body>Hello!</html>'
 
app.listen 3000

get, post, put, delete, options, patch

connect, trace, copy, lock, mkcol, move, propfind, proppatch, unlock, report, mkactivity, checkout, merge

Use router.route to create a custom route. route takes the arguments

  • method
  • path
  • mimetype
  • middleware
app = connect()
app.use connect.logger 'dev'
app.use router
 
router.route 'custom''/''*/*'->
    "I'm custom!"
 
app.listen 3000