Skip to content
πŸ”€ Utility class to register routes and dynamically generate their URL or path from anywhere in the express app
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
package.json
tsconfig.json
tslint.json
yarn.lock

README.md

Express Routes Archive

Build Status NPM Package Version Maintainability Test Coverage

Utility class to register routes and dynamically generate their URL or path from anywhere in the express app.

Installation

yarn add @xpbytes/express-routes-archive

Usage

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-arg

When 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/two

Use RoutesArchive#url(route, req) to generate full urls instead of path only:

root.url('penthouse', req)
// => https://test.xpbytes.com/level/over-9000

Configuration

  • SSL_ENABLED env to thruthy to make generated urls https.
  • SERVER_URL env to mount the path onto that URL instead of the request hostname.
You can’t perform that action at this time.