BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Expose router? #56

Closed JaneJeon closed 5 years ago

JaneJeon commented 5 years ago

Hi, is there any way to expose the router generated from restana?

I'm mainly interested in doing a router.prettyPrint() as supported by find-my-way, but I couldn't find any property/method of the service that returns the router.

jkyberneees commented 5 years ago

Hi @JaneJeon, you can achieve this by passing your own routerFactory and therefore having control of the router instance. Please see the example below:

let prettyPrint

const router = require('find-my-way')
const routerFactory = (options) => {
  const instance = router({
    ignoreTrailingSlash: options.ignoreTrailingSlash || false,
    allowUnsafeRegex: options.allowUnsafeRegex || false,
    maxParamLength: options.maxParamLength || 100,
    defaultRoute: options.defaultRoute || ((req, res) => res.send(404))
  })

  prettyPrint = () => instance.prettyPrint()

  return instance
}

const restana = require('restana')
const app = restana({
  routerFactory
})
...

You might also be interested on the routes() method in restana. Not that fancy as prettyPrint, but I guess will give you the same information.

Regards