JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 203
  • Score
    100M100P100Q93515F
  • 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

Fast lisp-like router for streaming servers.

Usage

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

const router = serverRouter([
  ['/hello', (req, res) => res.end('hello world')],
  ['/:username', {
    get: (req, res, params) => res.end(`username is ${params.username}`),
    delete: (req, res, params) => res.end(`${params.username} was 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([
  ['/index.html', (req, res) => fs.createReadStream('./index.html')],
  ['/404', (req, res) => fromString('404: file not found')]
])

http.createServer((req, res) => router(req, res).pipe(res)).listen()

API

router = serverRouter(opts, routes)

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.
  • thunk: (default: true) Change the way callbacks are wrapped internally. Unless you're wrapping sheet-router with some custom logic you probably don't want to call this.

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

See Also

License

MIT