Git Product home page Git Product logo

server-router's Introduction

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({ default: '/404' })

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

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

router.route('', '/404', function (req, res, params) {
  res.status = 404
  res.end('404')
})

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

API

router = serverRouter(opts)

Create a new router with opts.

router.route(method|[methods], route, function (req, res, params))

Register a new route with an HTTP method name and a routename. Can register multiple handlers by passing an array of method names. params contains matched partials from the route.

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

server-router's People

Contributors

ahdinosaur avatar f-mer avatar greenkeeperio-bot avatar lrlna avatar mahmoudelbadry avatar perguth avatar timwis avatar yerkopalma avatar yoshuawuyts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

server-router's Issues

Master branch fails tests.

$ npm test

# should register a default path
/workspace/server-router/node_modules/wayfarer/index.js:69
    throw new Error("route '" + route + "' did not match")
    ^

Error: route 'GET/bar' did not match
    at match (/workspace/server-router/node_modules/wayfarer/index.js:69:11)
    at ServerRouter.emit [as _router] (/workspace/server-router/node_modules/wayfarer/index.js:49:19)
    at ServerRouter.match (/workspace/server-router/index.js:40:15)
    at Server.handler (/workspace/server-router/test.js:56:14)
    at Server.emit (events.js:189:13)
    at parserOnIncoming (_http_server.js:676:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
npm ERR! Test failed.  See above for more details.

server-gateway and introspection support

The example mention "api-gateway" and link to "server-gateway", which points to nowhere.

It seems that api-gateway/server-gateway can walk through all the declared routes, and you can attach metadata to them! Can you please document the API for doing so?

expand to more match types

Cater for the real-world use case where validation, error handling and other things are accounted for. This would be an expanded version of the router that is now in place. This way we can, yet again, provide an upgrade path for people building stuff. Also because it happens on register-time, it will only be run and then be available for free. Easy!

https://gist.github.com/yoshuawuyts/4488f39c4a81a5a84b75

Tricks to keep it from crashing node?

Just out of curiosity

If you have like

  router.route('GET', '/api/*', fourofour)
  router.route('GET', '/api/bar', handle) 
  router.route('GET', '*', index)

  http.createServer(router.start())
    .listen(9000, function () {
      console.log(`http://localhost:${this.address().port}  ready`)
    })

/api/bar/x crashes

Do I just need to remember to add a catchall for every level of route?

router.route('GET', '/api/bar/*', fourofour)

exit Error: route 'GET/api/bar/x' did not match
    at match (/Users/travis/experiment/node_modules/wayfarer/index.js:69:11)
    at ServerRouter.emit [as _router] (/Users/travis/experiments/zt-spawn2/node_modules/wayfarer/index.js:49:19)
    at ServerRouter.match (/Users/travis/experiments/zt-spawn2/node_modules/server-router/index.js:40:15)
    at Server.<anonymous> (/Users/travis/experiments/zt-spawn2/node_modules/server-router/index.js:46:10)
    at Server.emit (events.js:160:13)
    at parserOnIncoming (_http_server.js:628:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.