Giter VIP home page Giter VIP logo

find-my-way's Introduction

find-my-way

js-standard-style Node CI NPM downloads

A crazy fast HTTP router, internally uses an highly performant Radix Tree (aka compact Prefix Tree), supports route params, wildcards, and it's framework independent.

If you want to see a benchmark comparison with the most commonly used routers, see here.
Do you need a real-world example that uses this router? Check out Fastify or Restify.

Install

npm i find-my-way --save

Usage

const http = require('http')
const router = require('find-my-way')()

router.on('GET', '/', (req, res, params) => {
  res.end('{"message":"hello world"}')
})

const server = http.createServer((req, res) => {
  router.lookup(req, res)
})

server.listen(3000, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')
})

API

FindMyWay([options])

Instance a new router.
You can pass a default route with the option defaultRoute.

const router = require('find-my-way')({
  defaultRoute: (req, res) => {
    res.statusCode = 404
    res.end()
  }
})

In case of a badly formatted url (eg: /hello/%world), by default find-my-way will invoke the defaultRoute, unless you specify the onBadUrl option:

const router = require('find-my-way')({
  onBadUrl: (path, req, res) => {
    res.statusCode = 400
    res.end(`Bad path: ${path}`)
  }
})

Trailing slashes can be ignored by supplying the ignoreTrailingSlash option:

const router = require('find-my-way')({
  ignoreTrailingSlash: true
})
function handler (req, res, params) {
  res.end('foo')
}
// maps "/foo/" and "/foo" to `handler`
router.on('GET', '/foo/', handler)

Duplicate slashes can be ignored by supplying the ignoreDuplicateSlashes option:

const router = require('find-my-way')({
  ignoreDuplicateSlashes: true
})
function handler (req, res, params) {
  res.end('foo')
}
// maps "/foo", "//foo", "///foo", etc to `handler`
router.on('GET', '////foo', handler)

Note that when ignoreTrailingSlash and ignoreDuplicateSlashes are both set to true, duplicate slashes will first be removed and then trailing slashes will, meaning //a//b//c// will be converted to /a/b/c.

You can set a custom length for parameters in parametric (standard, regex and multi) routes by using maxParamLength option, the default value is 100 characters.
If the maximum length limit is reached, the default route will be invoked.

const router = require('find-my-way')({
  maxParamLength: 500
})

If you are using a regex based route, find-my-way will throw an error if detects potentially catastrophic exponential-time regular expressions (internally uses safe-regex2).
If you want to disable this behavior, pass the option allowUnsafeRegex.

const router = require('find-my-way')({
  allowUnsafeRegex: true
})

According to RFC3986, find-my-way is case sensitive by default. You can disable this by setting the caseSensitive option to false: in that case, all paths will be matched as lowercase, but the route parameters or wildcards will maintain their original letter casing. You can turn off case sensitivity with:

const router = require('find-my-way')({
  caseSensitive: false
})

The default query string parser that find-my-way uses is fast-querystring module. You can change this default setting by passing the option querystringParser and use a custom one, such as qs.

const qs = require('qs')
const router = require('find-my-way')({
  querystringParser: str => qs.parse(str)
})

router.on('GET', '/', (req, res, params, store, searchParams) => {
  assert.equal(searchParams, { foo: 'bar', baz: 'faz' })
})

router.lookup({ method: 'GET', url: '/?foo=bar&baz=faz' }, null)

According to RFC3986, find-my-way separates path and query string with ? character. But earlier versions also used ; as delimiter character. To support this behaviour, add the useSemicolonDelimiter option to true:

const router = require('find-my-way')({
  useSemicolonDelimiter: true
})

You can assign a buildPrettyMeta function to sanitize a route's store object to use with the prettyPrint functions. This function should accept a single object and return an object.

const privateKey = new Symbol('private key')
const store = { token: '12345', [privateKey]: 'private value' }

const router = require('find-my-way')({
  buildPrettyMeta: route => {
    const cleanMeta = Object.assign({}, route.store)

    // remove private properties
    Object.keys(cleanMeta).forEach(k => {
      if (typeof k === 'symbol') delete cleanMeta[k]
    })

    return cleanMeta // this will show up in the pretty print output!
  }
})

store[privateKey] = 'private value'
router.on('GET', '/hello_world', (req, res) => {}, store)

router.prettyPrint()

//└── / (-)
//    └── hello_world (GET)
//        • (token) "12345"

on(method, path, [opts], handler, [store])

Register a new route.

router.on('GET', '/example', (req, res, params, store, searchParams) => {
  // your code
})

Last argument, store is used to pass an object that you can access later inside the handler function. If needed, store can be updated.

router.on('GET', '/example', (req, res, params, store) => {
  assert.equal(store, { message: 'hello world' })
}, { message: 'hello world' })
Versioned routes

If needed, you can provide a version route constraint, which will allow you to declare multiple versions of the same route that are used selectively when requests ask for different version using the Accept-Version header. This is useful if you want to support several different behaviours for a given route and different clients select among them.

If you never configure a versioned route, the 'Accept-Version' header will be ignored. Remember to set a Vary header in your responses with the value you are using for defining the versioning (e.g.: 'Accept-Version'), to prevent cache poisoning attacks. You can also configure this as part your Proxy/CDN.

default

The default versioning strategy follows the semver specification. When using lookup, find-my-way will automatically detect the Accept-Version header and route the request accordingly. Internally find-my-way uses the semver-store to get the correct version of the route; advanced ranges and pre-releases currently are not supported.

Be aware that using this feature will cause a degradation of the overall performances of the router.

router.on('GET', '/example', { constraints: { version: '1.2.0' }}, (req, res, params) => {
  res.end('Hello from 1.2.0!')
})

router.on('GET', '/example', { constraints: { version: '2.4.0' }}, (req, res, params) => {
  res.end('Hello from 2.4.0!')
})

// The 'Accept-Version' header could be '1.2.0' as well as '*', '2.x' or '2.4.x'

If you declare multiple versions with the same major or minor find-my-way will always choose the highest compatible with the Accept-Version header value.

custom

It's also possible to define a custom versioning strategy during the find-my-way initialization. In this case the logic of matching the request to the specific handler depends on the versioning strategy you use.

on(methods[], path, [opts], handler, [store])

Register a new route for each method specified in the methods array. It comes handy when you need to declare multiple routes with the same handler but different methods.

router.on(['GET', 'POST'], '/example', (req, res, params) => {
  // your code
})

Supported path formats

To register a parametric path, use the colon before the parameter name. For wildcard use the star. Remember that static routes are always inserted before parametric and wildcard.

// parametric
router.on('GET', '/example/:userId', (req, res, params) => {}))
router.on('GET', '/example/:userId/:secretToken', (req, res, params) => {}))

// wildcard
router.on('GET', '/example/*', (req, res, params) => {}))

Regular expression routes are supported as well, but pay attention, RegExp are very expensive in term of performance!
If you want to declare a regular expression route, you must put the regular expression inside round parenthesis after the parameter name.

// parametric with regexp
router.on('GET', '/example/:file(^\\d+).png', () => {}))

It's possible to define more than one parameter within the same couple of slash ("/"). Such as:

router.on('GET', '/example/near/:lat-:lng/radius/:r', (req, res, params) => {}))

Remember in this case to use the dash ("-") as parameters separator.

Finally it's possible to have multiple parameters with RegExp.

router.on('GET', '/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (req, res, params) => {}))

In this case as parameter separator it's possible to use whatever character is not matched by the regular expression.

The last parameter can be made optional if you add a question mark ("?") at the end of the parameters name.

router.on('GET', '/example/posts/:id?', (req, res, params) => {}))

In this case you can request /example/posts as well as /example/posts/1. The optional param will be undefined if not specified.

Having a route with multiple parameters may affect negatively the performance, so prefer single parameter approach whenever possible, especially on routes which are on the hot path of your application.

Note that you must encode the parameters containing reserved characters.

Match order

The routing algorithm matches one node at a time (where the node is a string between two slashes), this means that it cannot know if a route is static or dynamic until it finishes to match the URL.

The nodes are matched in the following order:

  1. static
  2. parametric node with static ending
  3. parametric(regex)/multi-parametric
  4. parametric
  5. wildcard

So if you declare the following routes

  • /foo/filename.png - static route
  • /foo/:filename.png - route with param filename and static ending .png
  • /foo/:filename.:ext - route with two params filename and ext
  • /foo/:filename - route with one param filename
  • /* - wildcard route

You will have next matching rules:

  • the static node would have the highest priority. It will be matched only if incoming URL equals /foo/filename.png
  • the parametric node with a static ending would have the higher priority than other parametric nodes without it. This node would match any filenames with .png extension. If one node static ending ends with another node static ending, the node with a longer static ending would have higher priority.
    • /foo/:filename.png.png - higher priority, more specific route
    • /foo/:filename.png - lower priority
  • the multi-parametric node (or any regexp node) without static ending would have lower priority than parametric node with static ending and higher priority than generic parametric node. You can declare only one node like that for the same route (see caveats). It would match any filenames with any extensions.
  • the parametric node has lower priority than any other parametric node. It would match any filenames, even if they don't have an extension.
  • the wildcard node has the lowest priority of all nodes.

Once a url has been matched, find-my-way will figure out which handler registered for that path matches the request if there are any constraints. find-my-way will check the most constrained handlers first, which means the handlers with the most keys in the constraints object.

If you just want a path containing a colon without declaring a parameter, use a double colon. For example, /name::customVerb will be interpreted as /name:customVerb

Supported methods

The router is able to route all HTTP methods defined by http core module.

off(methods[], path, [constraints])

Used to deregister routes.

off(methods, path)

If no constraint argument is passed, all routes with identical path and method are deregistered, regardless of whether a route has constraints or not.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('GET', '/example', { constraints: { version: '1.x' } })
router.on('GET', '/example')

// Deregisters all 3 routes registered above
router.off('GET', '/example')
off(methods, path, constraints)

If a constraint object is specified, only those routes are deleted that have the same constraints as well as the identical path and method. If an empty object is passed, only unconstrained routes will be deleted.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('GET', '/example', { constraints: { version: '1.x' } })
router.on('GET', '/example')

// Deregisters only the third route without constraints
router.off('GET', '/example', {})

// Deregisters only the first route
router.off('GET', '/example', { host: 'fastify.io' })
off(methods[], path)

Deregister a route for each method specified in the methods array. It comes handy when you need to deregister multiple routes with the same path but different methods. As explained above, the constraints will be ignored here.

router.on('GET', '/example', { constraints: { host: 'fastify.io' } })
router.on('POST', '/example', { constraints: { version: '1.x' } })
router.on('PUT', '/example')

// Deregisters all 3 routes registered above
router.off(['POST', 'GET', 'PUT'], '/example')
off(methods[], path, constraints)
router.on('GET', '/example', { constraints: { host: 'fastify.io' } }) // first route
router.on('POST', '/example', { constraints: { host: 'fastify.io' } }) // second route
router.on('POST', '/example', { constraints: { host: 'google.de' } }) // third route
router.on('GET', '/example') // fourth route 
router.on('POST', '/example') // fifth route 

// Deregisters only first and second route
router.off(['POST', 'GET'], '/example', { host: 'fastify.io' })

// Deregisters only fourth and fifth route
router.off(['POST', 'GET'], '/example', {})

findRoute (method, path, [constraints])

Finds a route by server route's path (not like find which finds a route by the url). Returns the route object if found, otherwise returns null. findRoute does not compare routes paths directly, instead it compares only paths patters. This means that findRoute will return a route even if the path passed to it does not match the route's path exactly. For example, if a route is registered with the path /example/:param1, findRoute will return the route if the path passed to it is /example/:param2.

const handler = (req, res, params) => {
  res.end('Hello World!')
}
router.on('GET', '/:file(^\\S+).png', handler)

router.findRoute('GET', '/:file(^\\S+).png')
// => { handler: Function, store: Object, params: ['file'] }

router.findRoute('GET', '/:file(^\\D+).jpg')
// => null
const handler = (req, res, params) => {
  res.end('Hello World!')
}
router.on('GET', '/:param1', handler)

router.findRoute('GET', '/:param1')
// => { handler: Function, store: Object, params: ['param1'] }

router.findRoute('GET', '/:param2')
// => { handler: Function, store: Object, params: ['param1'] }

hasRoute (method, path, [constraints])

Checks if a route exists by server route's path (see findRoute for more details). Returns true if found, otherwise returns false.

router.on('GET', '/:file(^\\S+).png', handler)

router.hasRoute('GET', '/:file(^\\S+).png')
// => true

router.hasRoute('GET', '/:file(^\\D+).jpg')
// => false

lookup(request, response, [context], [done])

Start a new search, request and response are the server req/res objects.
If a route is found it will automatically call the handler, otherwise the default route will be called.
The url is sanitized internally, all the parameters and wildcards are decoded automatically.

router.lookup(req, res)

lookup accepts an optional context which will be the value of this when executing a handler

router.on('GET', '*', function(req, res) {
  res.end(this.greeting);
})
router.lookup(req, res, { greeting: 'Hello, World!' })

lookup accepts an optional done callback for case when you use an async deriveConstraint function.

router.on('GET', '*', function(req, res) {
  res.end({ hello: 'world' });
})
router.lookup(req, res, (err) => {
  if (err !== null) {
    // handle error
  }
  console.log('Handler executed!!!'); 
})

find(method, path, [constraints])

Return (if present) the route registered in method:path.
The path must be sanitized, all the parameters and wildcards are decoded automatically.
An object with routing constraints should usually be passed as constraints, containing keys like the host for the request, the version for the route to be matched, or other custom constraint values. If the router is using the default versioning strategy, the version value should be conform to the semver specification. If you want to use the existing constraint strategies to derive the constraint values from an incoming request, use lookup instead of find. If no value is passed for constraints, the router won't match any constrained routes. If using constrained routes, passing undefined for the constraints leads to undefined behavior and should be avoided.

router.find('GET', '/example', { host: 'fastify.io' })
// => { handler: Function, params: Object, store: Object}
// => null

router.find('GET', '/example', { host: 'fastify.io', version: '1.x' })
// => { handler: Function, params: Object, store: Object}
// => null

prettyPrint([{ commonPrefix: false, includeMeta: true || [] }])

find-my-way builds a tree of routes for each HTTP method. If you call the prettyPrint without specifying an HTTP method, it will merge all the trees to one and print it. The merged tree does't represent the internal router structure. Don't use it for debugging.

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint())
// └── /
//     ├── test (GET)
//     │   ├── /hello (GET)
//     │   └── ing (GET)
//     │       └── /
//     │           └── :param (GET)
//     └── update (PUT)

If you want to print the internal tree, you can specify the method param. Printed tree will represent the internal router structure. Use it for debugging.

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint({ method: 'GET' }))
// └── /
//     └── test (GET)
//         ├── /hello (GET)
//         └── ing (GET)
//             └── /
//                 └── :param (GET)

console.log(findMyWay.prettyPrint({ method: 'PUT' }))
// └── /
//     └── update (PUT)

prettyPrint accepts an optional setting to print compressed routes. This is useful when you have a large number of routes with common prefixes. Doesn't represent the internal router structure. Don't use it for debugging.

console.log(findMyWay.prettyPrint({ commonPrefix: false }))
// ├── /test (GET)
// │   ├── /hello (GET)
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

To include a display of the store data passed to individual routes, the option includeMeta may be passed. If set to true all items will be displayed, this can also be set to an array specifying which keys (if present) should be displayed. This information can be further sanitized by specifying a buildPrettyMeta function which consumes and returns an object.

findMyWay.on('GET', '/test', () => {}, { onRequest: () => {}, authIDs: [1, 2, 3] })
findMyWay.on('GET', '/test/hello', () => {}, { token: 'df123-4567' })
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})

console.log(findMyWay.prettyPrint({ commonPrefix: false, includeMeta: ['onRequest'] }))
// ├── /test (GET)
// │   • (onRequest) "onRequest()"
// │   ├── /hello (GET)
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

console.log(findMyWay.prettyPrint({ commonPrefix: false, includeMeta: true }))
// ├── /test (GET)
// │   • (onRequest) "onRequest()"
// │   • (authIDs) [1,2,3]
// │   ├── /hello (GET)
// │   │   • (token) "df123-4567"
// │   └── ing (GET)
// │       └── /:param (GET)
// └── /update (PUT)

reset()

Empty router.

router.reset()

routes

Return the all routes registered at moment, useful for debugging.

const findMyWay = require('find-my-way')()

findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})

console.log(findMyWay.routes)
// Will print
// [
//   {
//     method: 'GET',
//     path: '/test',
//     opts: {},
//     handler: [Function],
//     store: undefined
//   },
//   {
//     method: 'GET',
//     path: '/test/hello',
//     opts: {},
//     handler: [Function],
//     store: undefined
//   }
// ]

Caveats

  • It's not possible to register two routes which differs only for their parameters, because internally they would be seen as the same route. In a such case you'll get an early error during the route registration phase. An example is worth thousand words:
const findMyWay = FindMyWay({
  defaultRoute: (req, res) => {}
})

findMyWay.on('GET', '/user/:userId(^\\d+)', (req, res, params) => {})

findMyWay.on('GET', '/user/:username(^[a-z]+)', (req, res, params) => {})
// Method 'GET' already declared for route ':'

Shorthand methods

If you want an even nicer api, you can also use the shorthand methods to declare your routes.

For each HTTP supported method, there's the shorthand method. For example:

router.get(path, handler [, store])
router.delete(path, handler [, store])
router.head(path, handler [, store])
router.patch(path, handler [, store])
router.post(path, handler [, store])
router.put(path, handler [, store])
router.options(path, handler [, store])
// ...

If you need a route that supports all methods you can use the all api.

router.all(path, handler [, store])

Constraints

find-my-way supports restricting handlers to only match certain requests for the same path. This can be used to support different versions of the same route that conform to a semver based versioning strategy, or restricting some routes to only be available on hosts. find-my-way has the semver based versioning strategy and a regex based hostname constraint strategy built in.

To constrain a route to only match sometimes, pass constraints to the route options when registering the route:

findMyWay.on('GET', '/', { constraints: { version: '1.0.2' } }, (req, res) => {
  // will only run when the request's Accept-Version header asks for a version semver compatible with 1.0.2, like 1.x, or 1.0.x.
})

findMyWay.on('GET', '/', { constraints: { host: 'example.com' } }, (req, res) => {
  // will only run when the request's Host header is `example.com`
})

Constraints can be combined, and route handlers will only match if all of the constraints for the handler match the request. find-my-way does a boolean AND with each route constraint, not an OR.

find-my-way will try to match the most constrained handlers first before handler with fewer or no constraints.

Custom Constraint Strategies

Custom constraining strategies can be added and are matched against incoming requests while trying to maintain find-my-way's high performance. To register a new type of constraint, you must add a new constraint strategy that knows how to match values to handlers, and that knows how to get the constraint value from a request. Register strategies when constructing a router or use the addConstraintStrategy method.

Add a custom constrain strategy when constructing a router:

const customResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the find-my-way route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay({ constraints: { accept: customResponseTypeStrategy } });

Add an async custom constrain strategy when constructing a router:

const asyncCustomResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the find-my-way route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx, done) => {
    done(null, req.headers['accept'])
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay({ constraints: { accept: asyncCustomResponseTypeStrategy } });

Add a custom constraint strategy using the addConstraintStrategy method:

const customResponseTypeStrategy = {
  // strategy name for referencing in the route handler `constraints` options
  name: 'accept',
  // storage factory for storing routes in the find-my-way route tree
  storage: function () {
    let handlers = {}
    return {
      get: (type) => { return handlers[type] || null },
      set: (type, store) => { handlers[type] = store }
    }
  },
  // function to get the value of the constraint from each incoming request
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  mustMatchWhenDerived: true
}

const router = FindMyWay();
router.addConstraintStrategy(customResponseTypeStrategy);

Once a custom constraint strategy is registered, routes can be added that are constrained using it:

findMyWay.on('GET', '/', { constraints: { accept: 'application/fancy+json' } }, (req, res) => {
  // will only run when the request's Accept header asks for 'application/fancy+json'
})

findMyWay.on('GET', '/', { constraints: { accept: 'application/fancy+xml' } }, (req, res) => {
  // will only run when the request's Accept header asks for 'application/fancy+xml'
})

Constraint strategies should be careful to make the deriveConstraint function performant as it is run for every request matched by the router. See the lib/strategies directory for examples of the built in constraint strategies.

By default, find-my-way uses a built in strategies for the version constraint that uses semantic version based matching logic, which is detailed below. It is possible to define an alternative strategy:

const customVersioning = {
  // replace the built in version strategy
  name: 'version',
  // provide a storage factory to store handlers in a simple way
  storage: function () {
    let versions = {}
    return {
      get: (version) => { return versions[version] || null },
      set: (version, store) => { versions[version] = store }
    }
  },
  deriveConstraint: (req, ctx) => {
    return req.headers['accept']
  },
  mustMatchWhenDerived: true, // if the request is asking for a version, don't match un-version-constrained handlers
  validate (value) {  // optional validate function, validates the assigned value at route-configuration (the .on function) time (not the runtime-value)
    assert(typeof value === 'string', 'Version should be a string')
  }
}

const router = FindMyWay({ constraints: { version: customVersioning } });

The custom strategy object should contain next properties:

  • storage - a factory function to store lists of handlers for each possible constraint value. The storage object can use domain-specific storage mechanisms to store handlers in a way that makes sense for the constraint at hand. See lib/strategies for examples, like the version constraint strategy that matches using semantic versions, or the host strategy that allows both exact and regex host constraints.
  • deriveConstraint - the function to determine the value of this constraint given a request

The signature of the functions and objects must match the one from the example above.

Please, be aware, if you use your own constraining strategy - you use it on your own risk. This can lead both to the performance degradation and bugs which are not related to find-my-way itself!

Acknowledgements

It is inspired by the echo router, some parts have been extracted from trekjs router.

Past sponsor

License

find-my-way - MIT
trekjs/router - MIT

Copyright © 2017-2019 Tomas Della Vedova

find-my-way's People

Contributors

absolux avatar airhorns avatar allevo avatar billouboq avatar climba03003 avatar delvedor avatar dependabot[bot] avatar disintegrator avatar eomm avatar fdawgs avatar hekike avatar ilteoood avatar ipalibowhyte avatar ivan-tymoshenko avatar jsumners avatar luisorbaiceta avatar mahovich avatar matthyk avatar mcollina avatar misterdjules avatar mse99 avatar nahuef avatar nooop3 avatar nwoltman avatar orangexc avatar pavelpolyakov avatar polymathca avatar rafaelgss avatar thenoim avatar uzlopak 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

find-my-way's Issues

parameters' case not preserved in case insensitive mode

When passing parameters values to route handlers, find-my-way doesn't preserve the case of the actual parameters:

$ cat index.js 
const util = require('util');

const router = require('find-my-way')({
  caseSensitive: false
});

const routePatterns = [
  '/foo/:bar'
];

routePatterns.forEach(function addRoute(routePattern) {
  router.on('GET', routePattern, (req, res, params) => {
    if (params) {
      console.log('params:', util.inspect(params, {depth: null}));
    }
  });

});

router.lookup({ method: 'GET', url: '/FOO/BAR', headers: {} }, null);
$ node --version
v6.14.3
$ node index.js 
params: { bar: 'bar' }
$ 

In the output above, I would expect the value of the parameter bar to be BAR, not bar.

Looking at some of the case insensitive tests, this seems intentional. However in general it seems counter-intuitive.

It seems it'd make more sense to preserve the original case and let the customer of find-my-way figure out whether and how they want to normalize the parameters values.

Thoughts?

If that makes sense, I have a set of changes that fixes this issue that I can submit as a PR asap.

Coverage status badge broken?

The coverage badge in the README doesn't show, and clickling on it leads to something without builds. Typo in the badge? Or WIP?

case insensitive mode errors when installing valid routes that are not all lowercase

Here's a repro of the problem I'm seeing:

$ cat index.js 
const router = require('find-my-way')({
  caseSensitive: false
});

const routePatterns = [
  '/Foo/bar/:baz',
  '/Foo/baz/:bar',
];

routePatterns.forEach(function addRoute(routePattern) {
  console.log('adding route for pattern:', routePattern);

  router.on('GET', routePattern, (req, res, params) => {
    res.end('{"message":"hello world"}')
  });
});


$ npm ls find-my-way
[email protected] /Users/jgilli/dev/test-find-my-way
└── [email protected] 

$ node --version
v6.14.3
$ node index.js 
adding route for pattern: /Foo/bar/:baz
adding route for pattern: /Foo/baz/:bar

assert.js:84
  throw new assert.AssertionError({
  ^
AssertionError: There is already a child with label ':'
    at Node.addChild (/Users/jgilli/dev/test-find-my-way/node_modules/find-my-way/node.js:57:3)
    at Router._insert (/Users/jgilli/dev/test-find-my-way/node_modules/find-my-way/index.js:249:19)
    at Router._on (/Users/jgilli/dev/test-find-my-way/node_modules/find-my-way/index.js:145:21)
    at Router.on (/Users/jgilli/dev/test-find-my-way/node_modules/find-my-way/index.js:58:8)
    at addRoute (/Users/jgilli/dev/test-find-my-way/index.js:13:10)
    at Array.forEach (native)
    at Object.<anonymous> (/Users/jgilli/dev/test-find-my-way/index.js:10:15)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
$ 

Changing those routes to be all lowercase seems to work around the problem. Not passing {caseSensitive: false} to the Router constructor also works around it.

RegExp routing issue

After commit 5899a60 the following route stopped working:

const router = require('find-my-way')()

router.get('/public/?.*', function fn () { })
router.find('GET', '/public/index.html/').params // {'*': ''}

Related issue #46

error: "There is already a child with label '*' "

[There is already a child with label '*']
is thrown in assertion if you do more than 1 wildcards using *.xx


server.get('/*.js', plugins.serveStatic({
  directory: './dist/valkyrie/',
  default: 'aaa.html'
}));

server.get('/*.css', plugins.serveStatic({
  directory: './dist/valkyrie/',
  default: 'aaa.html'
}));

Do you want to use pathlizer?

I've extracted the logic we use in Router.prototype.on to parse the path in a separate module, pathlizer.

Pros

  • Separates the concern of parsing the path, and creating the tree
  • It handles better some edge cases, and gives meaningful errors when path in not valid

Cons

  • It's an external dependency

Do you think is it worth to use it, or do you prefer to maintain find-my-way deps-free? Eventually I can try to fire a PR in the next days...

URL matches unexpectedly when multiple routes have a similar prefix

#65 solved #59/#60, but it created this new bug:

var router = require('find-my-way')()

router.on('GET', '/b/', function b () {})
router.on('GET', '/b/bulk', function b_bulk () {})
router.on('GET', '/b/ulk', function b_ulk () {})

console.log(router.find('GET', '/bulk')) // { handler: [Function: b_ulk], ... }

#65 may not have been the right fix. It seems like something is going on with how prefixes are matched and might have something to do with the label property on nodes.

Unclosed regex spins the cpu for ever

The following unclosed regex makes find-my-way spin the cpu forever. Throwing an exception could be nicer :)

var find = require('find-my-way')()

find.get('/foo/:id(a', function () {})

repeated code for method and path validation

Hi! Newbie coder here trying to learn stuff. I noticed that this code to validate paths is repeated twice, once in Router.prototype.off and once in Router.prototype.on:

assert(typeof path === 'string', 'Path should be a string')
assert(path.length > 0, 'The path could not be empty')
assert(path[0] === '/' || path[0] === '*', 'The first character of a path should be `/` or `*`')

Also, this code to validate methods is repeated twice, once in Router.prototype.off and once in Router.prototype._on:

assert(typeof method === 'string', 'Method should be a string')
assert(httpMethods.indexOf(method) !== -1, `Method '${method}' is not an http method.`)

Would it make sense to refactor these into functions, e.g. validatePath() and validateMethod()?

Returns routes registered without prettyPrint

Hello!

Maybe can be interesting add to public API a method that returns the array of routes registered. ( this can help me in a plugin fastify - I know the hook onRoute, but this seems better. ).

Thx.

Better handling of wildcard routes

Currently we are suffering the following issue:
Given this route declaration

router.on('GET', '/test/*', () => {})

It will respond only to /test/at-lest.one.character, while /test/ will not work.
Is not that easy to solve at the moment, it will require a refactor on how we are handling the routes prefixes, because currently a prefix almost always contains the / at the end, and that is our issue.

I'll leave this here, as soon I have enough time I'll get into it.
If someone wants to help, please fire a pr!

Related: fastify/fastify#266

Registering multiple routes with the same prefix unintentionally creates new routes

Simplified case from fastify/fastify#786

var router = require('find-my-way')()

router.on('GET', '/b/bulk', function b_bulk () {})

console.log(router.find('GET', '/bulk')) // null

router.on('GET', '/b/', function b () {})

console.log(router.find('GET', '/bulk')) // { handler: [Function: b_bulk], params: {}, store: null }

Somehow the POST /bulk route exists after the POST /b/ route is registered.

router.prettyPrint() doesn't show that it exists though:

└── /
    └── b/ (GET)
        └── bulk (GET)

Looks like it has something to do with the fact that b/ and bulk both start with the letter b. If you change bulk to something that doesn't start with the letter b, it works as expected.

Auto-binded `route.lookup` method?

Hello,

I discover with quite a joy this module :-) It's nice to explain routing without banging the head of people with frameworks.

What do you think of providing an auto-binded route.lookup method? It would make it even easier to plug into a createServer call.

const {createServer} = require('http');
const router = require('find-my-way')();

createServer(router.lookup).listen(4000);

Adding and using names for the router

I propose to make the coolest opportunity - to set a name for the router, similar to how it was done for koa-router

The name can be set as follows:

router.on('GET', '/example', { name: 'example' }, (req, res, params) => {
  // your code
})

// OR

router.on('GET', '/employee/:userId', { name: 'user' }, (req, res, params) => {
  // your code
})

for use:

router.url('example');
// => "/example"

router.url('user', { userId: 2 });
// => "/employee/2"

There are many options for using URL generation, for example, it can be used to generate sitemap.xml, for redirect, and the coolest thing is to use it in html templates.

Why is this cool?
After changing the URL in the code, there is no need to additionally change anything at all! All links placed and used by the code using router.url will automatically change!

Now after changing the URL, you need to make more changes, for example, to make a search and replace in code, scan the site for broken links. But after changing the URL, you can forget to make additional changes at all =)

Cannot find route to use

    const http = require('http')
    const router = require('find-my-way')()

    router.on('GET', '/:namespace/:type/:id', (req, res, params) => {
        res.end('{"message":"hello world"}')
    })

    router.on('POST', '/:namespace/jobs/:name/run', (req, res, params) => {
        res.end('{"message":"hello world"}')
    })

    const server = http.createServer((req, res) => {
        router.lookup(req, res)
    })

    server.listen(4000, err => {
        if (err) throw err
        console.log('Server listening on: http://localhost:4000')
    })

Works as expected

GET http://localhost:4000/test_namespace/test_type/test_id
GET http://localhost:4000/test_namespace/jobss/test_id

Does not work

GET http://localhost:4000/test_namespace/jobs/test_id

support for multiple methods

It would be really nice to have support for routes with multiple methods (like Express' all() or Hapi's support of all methods as array).

Bug: parametrised regex being bypassed

The regex for route 2 and 3 of the below code shouldn't match but I am getting the following responses

Route Response
1 /non-trailing-path {"1: Non trailing path": { path1: 'non-trailing-path' }}
2 /non-localised/ {"2: trailing localised": { locale2: 'non-localised' }}
3 /non-localised/wildcard {"3: trailing localised with wildcard": { locale3: 'non-localised', '*': 'wildcard' }}
const http = require('http')
const inspect = require('util').inspect
const router = require('find-my-way')()

/* 1 */
router.on('GET', '/:path1', (req, res, params) => {
  res.end(`{"1: Non trailing path": ${inspect(params)}}`)
})
/* 2 */
router.on('GET', '/:locale2(^[a-z]{2}-[a-z]{2}$)/', (req, res, params) => {
  res.end(`{"2: trailing localised": ${inspect(params)}}`)
})
/* 3 */
router.on('GET', '/:locale3(^[a-z]{2}-[a-z]{2}$)/*', (req, res, params) => {
  res.end(`{"3: trailing localised with wildcard": ${inspect(params)}}`)
})

const server = http.createServer((req, res) => {
  router.lookup(req, res)
})

console.log(
    router.prettyPrint()
);
server.listen(3000, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')
})

Splitting on `;` instead of `?`

It looks like some vendors use ; instead of ? to separate the path from the query string. This is wrong per spec:

The query component contains non-hierarchical data that, along with
data in the path component (Section 3.3), serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any). The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
https://tools.ietf.org/html/rfc3986#section-3.4

But I can tell you that it is out there. So the following should also look for character code 59 as an initial separator.

if (charCode === 63 || charCode === 35) {

Support for multiple versions

Have you ever considered to add version support to find-my-way?
If I understand correctly it could be a new child node in a Radix Tree below the path node.

Something like this:

router.on('GET', '/', '1.2.3', (req, res, params) => {
  res.end('{"message":"hello world 1"}')
})
router.on('GET', '/', '2.0.0', (req, res, params) => {
  res.end('{"message":"hello world 2"}')
})

request({
  method: 'GET',
  uri: '/',
  headers: { 'accept-version': '~1.2'  }
}) => 'hello world 2'

request({
  method: 'GET',
  uri: '/',
  headers: { 'accept-version': '~3.1'  }
}) => 400, ~2.1 is not supported by GET /

Lookup could happen by Accept-Version header with finding the biggest version that satisfies via semver.

support Accept header for "versioning"

Hi @delvedor,

That's Pavel who contributed to fastify-swagger (if that's somehow important ;)).

I'd like to extend find-my-way to support "versioning" through the Accept header.

Here is some info:
http://labs.qandidate.com/blog/2014/10/16/using-the-accept-header-to-version-your-api/
https://youtu.be/mZ8_QgJ5mbs?t=1275 (starting from 21:15)

I don't know if I succeed, but I want to try. At the end I want to have this available in fastify.

Main question to you as maintainer of find-my-way at this stage - is this something you may merge in? Or, maybe, for you this has no sense at all.

Merry Christmas 🎅 and looking forward to your response.

Regards,

Route matching is not falling back to parametric route

I'm running Restify 7.x in production and I was able to reproduce an issue I discovered down here.

Consider the following:

const http = require('http')
const router = require('find-my-way')()

router.on('GET', '/a/bbbb', (req, res) => {
  res.end('{"message":"hello world"}')
})

router.on('GET', '/a/bbaa', (req, res) => {
  res.end('{"message":"hello world"}')
})

router.on('GET', '/a/babb', (req, res) => {
  res.end('{"message":"hello world"}')
})

router.on('DELETE', '/a/:id', (req, res) => {
  res.end('{"message":"hello world"}')
})

console.log(router.find('DELETE', '/a/bar')); 
// => { handler: [Function], params: { id: 'bar' }, store: null }

console.log(router.find('DELETE', '/a/bbar')); 
// => null

console.log(router.prettyPrint());
// └── /
//     └── a/
//         ├── b
//         │   ├── b
//         │   │   ├── bb (GET)
//         │   │   └── aa (GET)
//         │   └── abb (GET)
//         └── :id (DELETE)

const server = http.createServer((req, res) => {
  router.lookup(req, res)
})

server.listen(3040, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3040')
})

If router.on('GET', '/a/bbaa',...) is taking out, DELETE /a/bbar seems to be matched otherwise, I get a 404. Any ideas why this is happening?

Thanks in advance!

404 on valid routes

Hi,

I'm getting 404s on paths matching existing routes, it looks similar to #101 but I'm not sure it's the same issue so I'm creating a new one.

I only have a repro with fastify involved, hope you don't mind: https://runkit.com/embed/lx7pizmbgoru
There are several weird things in my example like a completely different path influencing the outcome, but it looks like the issue is with the variable part of the url starting like letters of another static route, at least that's my best guess.

I bisected until find-my-way 1.0.0 but it seems it's always been there, so it's not a regression.

Unexpected route order

Hi!

The readme explains very well the route order.
In particular the parametric routes go before the parametric ones with regexp.

Using the following code,

function handler (req, res, params) {
  console.log(params)
}
const findMyWay = new FindMyWay({ defaultRoute: notFoundHandler })

findMyWay.on('GET', '/:userId/foo/bar', handler)
findMyWay.on('GET', '/33/:a(^.*$)/:b', handler)

findMyWay.lookup({ url: '/33/foo/bar', method: 'GET', headers: {} }, _res)

The regexp route should come after the parametric one and the expected output should be

{ userId: "33" }

Instead the output is

{ a: 'foo', b: 'bar' }

Am I wrong in reading the readme?

I'm able to push a failing test in a PR if it helps

'*' does not catch all routes.

The following code will not work as expected:

router.on('GET', '*', () => {}))

router.lookup(req, res)

The lookup will call the default route, because find-my-way does not split the first '/' from the url path.

Related: fastify/fastify#81

README snippet typo

Hi!

There is an error in this snippet:

router.on('GET', '/example', { version: '1.2.0' }, (req, res, params) => {
  res.send('Hello from 1.2.0!')
})

router.on('GET', '/example', { version: '2.4.0' }, (req, res, params) => {
  res.send('Hello from 2.4.0!')
})

// The 'Accept-Version' header could be '1.2.0' as well as '*', '2.x' or '2.4.x'

res.send it's not a valid function, since send is an Express and/or Restify API. It should be res.end.

Support `ignoreTrailingSlash` in options for `.on()`

While working on fastify/fastify-static#93 I found that the new option is sometimes incompatible with ignoreTrialingSlash: true. I'd like to propose that it should be possible to disable this on specific calls to router.on():

const router = require('find-my-way')({ignoreTrialingSlash: true})
router.on('GET', '/foo', handlerFoo) // register '/foo' and '/foo/' per global option
router.on('GET', '/bar', {ignoreTrialingSlash: false}, handlerBar) // do not attempt to register '/bar/'
router.on('GET', '/bar/', {ignoreTrialingSlash: false}, handlerBarSlash) // do not attempt to register '/bar'

Would you accept such a feature?

Found a wrong route

The following code finds a wrong route

const noop = () => {}
const findMyWay = FindMyWay()

findMyWay.on('GET', '/bb/', noop)
findMyWay.on('GET', '/bb/bulk', noop)

const f = findMyWay.find('GET', '/bulk')

// f === noop

Expected:
f should be null

Issue born here: fastify/fastify#787

Got 404 Using curl -I

Following code always return 404 using this command curl -I http://localhost:6000/

const http = require('http');
const router = require('find-my-way')();

router.on('GET', '/', (req, res) => {
  res.writeHead(200);
  res.end('<b>OK</b>');
});

const server = http.createServer((req, res) => {
  router.lookup(req, res);
});

server.listen(6000);

The response header is:

HTTP/1.1 404 Not Found
Date: Tue, 12 Sep 2017 10:47:43 GMT
Connection: keep-alive

Why?

issue with path validation

#30 causes this:

With this PR in place I get the following error:

  1) Server#player "before all" hook: start webserver:

      Uncaught AssertionError [ERR_ASSERTION]: The path could not be empty
      + expected - actual

      -false
      +true
      
      at Router.on (node_modules/find-my-way/index.js:53:3)
      at Router.on (node_modules/find-my-way/index.js:43:12)
      at Router.all (node_modules/find-my-way/index.js:360:8)
      at Function._setNotFoundHandler (node_modules/fastify/fastify.js:602:18)
      at after (node_modules/fastify/fastify.js:559:27)
      at Function._encapsulateThreeParam (node_modules/avvio/boot.js:268:7)
      at Boot.callWithCbOrNextTick (node_modules/avvio/boot.js:223:5)
      at Boot._after (node_modules/avvio/boot.js:180:26)

Likely fails here: https://github.com/fastify/fastify/blob/master/fastify.js#L594-L598

Specifically for these instructions so as not to poke around repos:

      var prefix = this._RoutePrefix.prefix
      var star = '/*'

      fourOhFour.all(prefix + star, startHooks, store)  // <-- this seem to be failing
      fourOhFour.all(prefix || '/', startHooks, store)

Where prefix is '' - i haven't yet added a test-case to verify this, and why it happens, and maybe it simply should be * instead of /* in the fastify, but well, still a comment here!

AssertionError: Method 'GET' already declared for route

I came across this (still failing) test case while thinking a general solution for the issue #29 (comment)

test('Similar route path, different parameter', t => {
  t.plan(2)
  const findMyWay = FindMyWay({
    defaultRoute: (req, res) => {
      t.fail('we should not be here, the url is: ' + req.url)
    }
  })
  findMyWay.on('GET', '/user/:userId(^\\d+)/orders', (req, res, params) => {
    t.equal(params.userId, '42')
  })
  findMyWay.on('GET', '/user/:username(^[a-z]+)/orders', (req, res, params) => {
    t.equal(params.username, 'foo')
  })
  findMyWay.lookup({ method: 'GET', url: '/user/42/orders' }, null)
  findMyWay.lookup({ method: 'GET', url: '/user/foo/orders' }, null)
})

Currently it's not possible to configure a routing such as the one above:
AssertionError: Method 'GET' already declared for route '/orders'.

Do you think find-my-way should support this use case?

use methods lib

const  methods = require('methods')
var httpMethods = []
methods.forEach(method => {
  httpMethods.push(method.toUpperCase())
})

methods.forEach(function(method){
  Router.prototype[method] = function(path, handler, store){
    return this.on(method.toUpperCase(), path, handler, store)
  };
});

support for multiple params

I think the current problem for #15 is not about regexps. It's about multiple params in the same part:

/hello/:my-:world.html

Currently, find-my-way only supports one parameter within two /.

New option: redirect for trailing slashes

Find-my-way has a ignoreTrailingSlash option that allows to access the same page at the addresses "/foo/" and "/foo".

For SEO it would be more useful to add an option that would redirect all requests with a slash at the end of the URL to the same URL without a slash, ignoring the query string (key and value).

Request HTTP code Response
/foo 200 /foo
/foo/ 301 /foo
/foo?a=1 200 /foo?a=1
/foo/?a=1 301 /foo?a=1

For some users of the site (who change the URL in the address bar), it is important to support the processing of the final slash. It is also important not to duplicate the same page for search robots.
To solve this problem this new option would be very helpful.

For the Koa framework, there is middleware solving this task koa-no-trailing-slash.
But using it before using the find-my-way router creates another problem:
when accessing the non-existing page "/404/", a redirect will occur to the "/404" page and only then an error will occur - an extra redirect occurs here.

Request HTTP code Location
/404/ 301 /404
/404 404

If this task will be solved by find-my-way with a new option, then when accessing "/404/", an error page will immediately appear.

Request HTTP code Location
/404/ 404
/404 404

Therefore, would like to see this option in the router, and not to solve it with a separate functionality.

How do you look at adding such an option?

Using regex to capture the URL parameters

The following route definition works in Express.js, but does not work in Fastify/ find-my-way
, /cinema-logos/:cinemaId(\\d+)-:cinemaName.svg.

In Fastify, it produces params: { 'cinemaId(\d+)-:cinemaName.svg': '1000146-vue.svg' },, whereas I expect to get cinemaId and cinemaName as separate parameters.

:cinemaId-:cinemaName is not working either, because the matching expression appears to be greedy: 'cinemaId-:cinemaName.svg': '1000146-vue.svg'.

Idea: Allow registration of multiple routes at once through an array?

Does anyone else think it would nice to supply an array of routes to 'router.on([...])' to register them all with one call?
This would be useful if I want to define my sub/type-specific routes in other files, and have to import them somewhere else to be registered with the main router.
Instead of having to manually register each route, the sub-file could return an array of the routes, and the router could register them all with router.on(Array).
Granted, I could just pass the router to the sub-file, and manually register each route to it, which I'm doing, so this is just a nice-to-have I think.
Anyone see a downside to that?

Regex groups seem to break it

Had a regex with a group inside which breaks the parsing /foo/:id(([a-f0-9]{32},?)+)

var find = require('find-my-way')()

find.get('/foo/:id(([a-f0-9]{32},?)+)', function () {})

Defining static route after parametric route causes issue

Issue based on fastify/fastify#573

Works:

const router = require('find-my-way')()

router.on('GET', '/static', function s () {}) // Static route defined first
router.on('GET', '/:param', function p () {})

router.find('GET', '/static')
// { handler: [Function: s], params: {}, store: null }

router.find('GET', '/para')
// { handler: [Function: p], params: { param: 'para' }, store: null }

router.find('GET', '/s')
// { handler: [Function: p], params: { param: 's' }, store: null }

Does not work:

const router = require('find-my-way')()

router.on('GET', '/:param', function p () {})
router.on('GET', '/static', function s () {}) // Static route defined second

router.find('GET', '/static')
// { handler: [Function: s], params: {}, store: null }

router.find('GET', '/para')
// { handler: [Function: p], params: { param: 'para' }, store: null }

router.find('GET', '/s')
// null

Expected router.find('GET', '/s') to return the same object as above.

Related to #44 which was fixed (except for this issue) by #45.

Parametric path resolution

const http = require("http");
const router = require("find-my-way")();

router.on("GET", "/a/:param", (req, res, params) => res.end(JSON.stringify(params)));

http.createServer(router.lookup.bind(router)).listen(3000);

Works as expected

http://localhost:3000/a/perfectly-fine-route => {"param":"perfectly-fine-route"}

Does not work

Support for .off method to unmount a route

Would be nice to have an off method on the router instance to be able to unmount already attached handler.

const router = require('find-my-way')()

function handler (req, res, params) {
  res.end('{"message":"hello world"}')
}

router.on('GET', '/', handler)
router.off('GET', '/') // or router.off('GET', '/', handler)

prettyPrint outputs wrong indent

I found a glitch with prettyPrint.
Take the following code as an example:

fastify.get('/pet/:id', async function (req, rep) {})
fastify.post('/pet/:id', async function (req, rep) {})
fastify.get('/preview', async function (req, rep) {})

It will finally output like this, where the indent of the route post('/pet/:id') is not correct:
S9U~X@(K2%{EX AYE}IG9L

I know that prettyPrint is depreciated, but it seems just a small bug that can be easily fixed.

add koa && express support

code

const http = require('http')
const Koa = require('koa');
const app = new Koa();

const router = require('./')()

router.get('/', (ctx, next) => {
  ctx.body = {'path': 'root'}
})

router.on('GET', '/test', (ctx, next) => {
  ctx.body = {'hello': 'world'}
})

app.use(router.routes());

app.use(async function (ctx, next) {
  ctx.body = "default"
});

const server = http.createServer(app.callback())

server.listen(3030, err => {
  if (err) throw err
  console.log('Server listening on: http://localhost:3000')
})

https://github.com/moajs/moa-router

Wildcard route without trailing slash has wrong params

See this RunKit.

const router = require('find-my-way')()

router.get('/static/*', function fn () { })

router.find('GET', '/static/').params // {'*': ''}
router.find('GET', '/static').params // {'*': 'static'}

Expected params to be {'*': ''} both times (or /static not to match the route, similar to the functionality for /static/:param).

(I do think that find-my-way should treat routes the same whether they have a trailing slash or not, but that's a separate issue.)

Allow for identifying 404 vs 405

Whenever the router fails to match a request, .find() returns null for two reasons:

  • Method mismatch (405)
  • Path mismatch (404)

Is there a way to differentiate between the two in my handler to send the right HTTP error ?

Thanks

hey, how can i read file ? like index.html

i want to display index.html

router.on('GET', '/', (req, res, params) => {
res.readFileSync('./index.html',function(err,data){
if(err){res.statusCode = 404}else{
res.statusCode = 200
}
res.end(data)
})
})
it says res.readFileSync it s not a function

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.