JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 203
  • Score
    100M100P100Q93521F
  • License MIT

Server router

Package Exports

  • server-router
  • server-router/walk

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (server-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

server-router stability

npm version build status test coverage downloads js-standard-style

Performant radix-trie router for streaming servers.

Usage

var serverRouter = require('server-router')
var http = require('http')

var router = serverRouter()

router.route('GET', '/hello', function (req, res, ctx) {
  res.end('hello world')
})

router.route('PUT', '/hello/:name', function (req, res, ctx) {
  res.end('hi there ' + ctx.params.name)
})

http.createServer(router.start()).listen()

API

router = serverRouter(opts)

Create a new router with opts:

  • default: (default: '/404') Path to default to when a route is not matched. If no default path is set, the router will crash when an unknown path is encountered.

router.route(method|routes, route, function(req, res, ctx))

Register a new route with an HTTP method name and a routename. Can register multiple handlers by passing an array of method names. ctx is an object with .params property that contains any params. It's safe to extend ctx with other values for the duration of the request.

router.match(req, res)

Match a route on a router.

handler = router.start()

Return a function that can be passed directly to http.createServer() and calls router.match().

Installation

$ npm install server-router

See Also

License

MIT