3.0.0 • Published 5 years ago
@xpbytes/express-routes-archive v3.0.0
Express Routes Archive
Utility class to register routes and dynamically generate their URL or path from anywhere in the express app.
Installation
yarn add @xpbytes/express-routes-archiveIt has the following peer dependencies:
yarn add @types/express @types/express-serve-static-core @types/node -DUsage
import { RoutesArchive } from '@xpbytes/express-routes-archive'
const root = new RoutesArchive()
root.register('foo', '/test')
root.register(
'bar',
(mountedAt: string, arg: any) => `${mountedAt}/test?bar=${arg}`
)
root.path('test')
// => /test
root.path('bar', 'my-arg')
// => /test?bar=my-argWhen you have a sub-router / controller, create a new RoutesArchive, passing in the original and the mount path:
const up = new RoutesArchive('/level', root)
up.register('level', '/two')
up.register('penthouse', (mountedAt: string) => `${mountedAt}/over-9000`)
up.path('level')
// => /level/two
root.path('level')
// => /level/twoUse RoutesArchive#url(route, req) to generate full urls instead of path only:
root.url('penthouse', req)
// => https://test.xpbytes.com/level/over-9000Configuration
SSL_ENABLEDenv to thruthy to make generated urlshttps.SERVER_URLenv to mount the path onto that URL instead of the request hostname.