@xpbytes/express-routes-archive
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Express Routes Archive

npm NPM Package Version Maintainability

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

It has the following peer dependencies:

yarn add @types/express @types/express-serve-static-core @types/node -D

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @xpbytes/express-routes-archive

Weekly Downloads

2

Version

3.0.0

License

MIT

Unpacked Size

14.1 kB

Total Files

12

Last publish

Collaborators

  • sleeplessbyte
  • vincentrbbmnd
  • thexa4