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

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

Server router is a fast, modular server-side router. It's tuned for performance by statically declaring routes in a radix-trie. Can return values which makes it ideal for middleware pipelines using streams.

Usage

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

const router = serverRouter('/404')
router.on('/hello', function (req, res) {
  res.end('hello world')
})

router.on('/:username', {
  get: function (req, res, params) {
    res.end('username is ' + params.username)
  },
  delete: function (req, res, params) {
    res.end('username ' + params.username + 'will be deleted')
  }
})

http.createServer(router).listen()

Usage with streams

Server-router can return values, which makes it ideal for Node streams, pull-streams or other eventual values:

const serverRouter = require('server-router')
const fromString = require('from2-string')
const http = require('http')
const fs = require('fs')

const router = serverRouter('/404')
router.on('/index.html', function (req, res) {
  return fs.createReadStream(./index.hmlt)
})

router.on('/404', function (req, res) {
  res.statusCode = 404
  return fromString('File not found')
})

http.createServer(function (req, res) {
  router(req, res).pipe(res)
}).listen()

API

router = serverRouter(default)

Create a new router with a default path. If no default path is set, the router will crash if an unknown path is encountered.

router.on(route, callback|obj)

Attach a callback to a route. Callback can be either a function, which is registered as GET or an object containing functions, with valid HTTP methods as keys.

value = router(req, res, params, ...?)

Match a route on a router. Additional arguments can be passed to the matched function. Matched routes have a signature of (req, res, params, ...?). Match functions can return values, which is useful to create pipelines.

Installation

$ npm install server-router

License

MIT