The router allows to define EdgeJS rules and their associated features in convenient way as a chainable javascript code. Please pay attention to the following notes when using Router to avoid unexpected behavior.

Router evaluation

The defined EdgeJS rules are transformed to equivalent EdgeControl rules for the Edge only once during the build of Router when it's evaluated in Edgio's serverless cloud. Therefore, any other runtime modifications of the Router are not supported even though they are valid javascript code.

Example of incorrect usages:

 import { Router } = from '@edgio/core/router'

const router = new Router()
// This will not work!
// Router cannot be modified during runtime from the compute function.
.get('/products/:category/:id', ({ compute }) => {
compute(async (req, res) => {
if(req.path.startsWith('/product/books')){
router.static('products/books')
}
})
})
// This will not work as expected!
// The following code will add two independent rules, one for '/products/:category/:id' and second for '/products/books/:id',
// because the called get() methods don't know about nested structure. We strongly recommend to avoid this code pattern.
.get('/products/:category/:id', ({}) => {
router.get('/products/books/:id', {
origin: {
set_origin: "api"
}
})
})

export default router

Hierarchy

  • default

Constructors

Properties

functions: {
    [index: string]: ComputeFn;
} = {}

Type declaration

isIfActive: boolean = false

Variable that is true only if router is inside of 'if' statement.

NOTE: we could allow user to 'else' and 'elseif' before previous 'if' calls, as in many cases, it would be valid - bool was added to avoid having to handle invalid cases.

Invalid cases :

  • previous conditional with already existing 'else'
  • features-only rule, when createMatchers returns empty criteria

Also, it just seems wrong to allow calling something like .get().elseif()

preloadRequests: default = ...
routerOptions: RouterOptions = {}
rules: Rules[] = []
functionIndex: number = 0

Accessors

Methods

  • Adds a route that matches all requests that result in an error.

    Example:

     new Router()
    .catch(/^(4|5)\d{2}$/, {
    // retry all 4xx and 5xx errors using the legacy origin
    retry: {
    origin: "legacy",
    },
    });

    Returns

    A self-reference, suitable for chaining.

    Parameters

    • error: string | number | RegExp

      A regular expression, string or number that matches the status code returned by the origin

    • features: FeaturesParam

      Features to apply when a request matches the route

    Returns default

  • Registers a rule that supports advanced if/then/else logic.

    Returns

    A self reference, suitable for chaining

    Parameters

    Returns default

  • Forces the HTTPS protocol for all requests by redirecting HTTP requests to HTTPS and sets the Strict-Transport-Security header with the specified options to tell the browser to remember that this site is only to be accessed using HTTPS.

    Example:

     new Router()
    .forceHttps({
    redirectCode: 302,
    hsts: true,
    hstsMaxAge: 31536000,
    })
    .get('/api/products/:category/:id', ({ setOrigin }) => {
    setOrigin('api');
    })

    Returns

    A self-reference, suitable for chaining.

    Parameters

    • options: default = {}

    Returns this

  • Returns the static asset manifest from this router file. NOTE: This method is needed because we're dynamically loading another Router class from a bundled routes.js file, which doesn't share a same class static variables.

    Returns {
        [key: string]: string[];
    }

    • [key: string]: string[]
  • Disables crawling of permalinks by setting the x-robots-tag: noindex response header for hosts matching edgio.link or edgio-perma.link.

     new Router().noIndexPermalink()
    

    Deprecated

    Deprecated. Indexing permalinks is automatically disabled. Use indexPermalink?: boolean on Router Options to enable them. *

    Returns

    A self-reference, suitable for chaining.

    Returns default

  • Adds routes for all static assets in a directory tree.

    Returns

    A self-reference, suitable for chaining.

    Parameters

    • sourcePath: string

      The path to a directory containing static assets relative to the root of your project.

    • options: StaticOptions = {}

    Returns default

  • Registers a plugin that adds group of rules into the router. This method should be always called before any other methods that add rules. Example

     import { Router } = from '@edgio/core/router'
    import { nextRoutes } from '@edgio/next'

    export default new Router()
    // NextRoutes automatically adds rules for all Next.js pages and their assets.
    // This should be called before any custom rules are added.
    .use(nextRoutes)
    .get('/custom-rule', {
    origin: {
    set_origin: "api"
    }
    })

    Parameters

    Returns default

  • Parameters

    Returns string[]

  • Loads and returns a router from a file that exports it with module.exports or export default.

    Parameters

    • routerPath: string

      A path to routes.js,

    Returns default

Generated using TypeDoc