copperhead

Connect compatible router middleware that supports content negotiation

npm install copperhead
40 downloads in the last month

Copperhead

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

Installation

npm install copperhead

Usage

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

Available Methods

HTTP 1.1

get, post, put, delete, options, patch

WebDAV

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

Custom

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

Similar Projects

npm loves you