Options
All
  • Public
  • Public/Protected
  • All
Menu

The API that is provided to route callbacks.

Hierarchy

  • BaseResponseWriter
    • ResponseWriter

Index

Constructors

constructor

  • Parameters

    • req: Request

      The request

    • res: Response

      The response

    • route: Route

      The route

    Returns ResponseWriter

Properties

Protected route

route: Route

Methods

allowCors

  • Sends the necessary response headers to allow CORS

    Parameters

    • Default value config: CorsOptions = {}

      Example

       new Router()
         .match('/api/*path', ({ allowCors }) => {
           allowCors({
             origin: '*', // this is the default
             methods: ['get', 'post'],
             headers: ['x-some-header'],
             maxAge: 60 * 60, // one hour
             credentials: true
           })
         })

    Returns void

cache

  • Sets the caching behavior for both browser and edge.

    Example

     new Router()
       .get('/p/:productId', ({ cache, proxy }) => {
         cache({
           browser: {
             maxAgeSeconds: 0,
             serviceWorkerSeconds: 60 * 60, // 1 hour
           },
           edge: {
             maxAgeSeconds: 60 * 60 * 24 // 24 hours
             staleWhileRevalidateSeconds: 60 * 60 // 1 hour
           }
         })
         return proxy('origin')
       })

    The cache() method can be called in the same route where the response is sent, or any prior route. For example, with Next.js, it is common to use the next plugin to automatically inherit page routes based on Next.js conventions, and use the XDN router simply to add caching:

     import { Router } = from '@xdn/core/router'
     import { createNextPlugin } from '@xdn/next'
    
     const { nextMiddleware, renderNext } = createNextPlugin()
    
     new Router()
       .get('/p/:productId', ({ cache, proxy }) => {
         cache({
           browser: {
             maxAgeSeconds: 0,
             serviceWorkerSeconds: 60 * 60, // 1 hour
           },
           edge: {
             maxAgeSeconds: 60 * 60 * 24 // 24 hours
             staleWhileRevalidateSeconds: 60 * 60 // 1 hour
           }
         })
         return proxy('origin')
       })
       .use(nextMiddleware)

    Parameters

    Returns void

proxy

  • Relays the request to the specified backend.

    Example

     new Router()
       .get('/some/path/with/:variable', ({ proxy }) => {
         return proxy('legacy', { path: '/some/other/path/with/:variable' })
       })

    In this example, we relay the request to the "legacy" backend. In this case, xdn.config.js must contain a definition for the legacy backend. For example:

     // xdn.config.js
    
     module.exports = {
       backends: {
         legacy: {
           domainOrIp: 'legacy.domain.com',
           hostHeader: 'domain.com'
         }
       }
     }

    Parameters

    • backend: string

      The name of one of the backends in your xdn.config.js file.

    • Optional options: ProxyOptions

    Returns void

    A promise the resolves once the response has been fetched from the upstream site.

redirect

  • redirect(to: string, statusCode?: number): void
  • Redirects the browser to a new location.

    Example

     new Router()
       .get('/p/:productId', ({ redirect }) => {
         return redirect('/products/:productId', 301)
       })

    Parameters

    • to: string

      The URL to which the browser will be redirected.

    • Default value statusCode: number = 302

      The HTTP status to return. Defaults to 301

    Returns void

removeRequestHeader

  • removeRequestHeader(name: string): void
  • Removes a request header. This method should be called before proxy.

    Example

     new Router()
       .get('/some/path', async ({ removeRequestHeader, proxy }) => {
         removeRequestHeader('some-header')
         await proxy('origin')
       })

    Parameters

    • name: string

      The case-insensitive name of the request header

    Returns void

removeResponseHeader

  • removeResponseHeader(name: string): void
  • Removes a response header immediately before delivering the response downstream. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ removeResponseHeader, proxy }) => {
         await proxy('origin')
         removeResponseHeader('some-header')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    Returns void

removeUpstreamResponseHeader

  • removeUpstreamResponseHeader(name: string): void
  • Removes a response header before attempting to cache the response. In local development this is same as calling updateUpstreamResponseHeader but on edge it removes a header immediately after the upstream fetch has finished. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ removeUpstreamResponseHeader, proxy }) => {
         await proxy('origin')
         removeUpstreamResponseHeader('set-cookie')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    Returns void

render

  • render(cb: (request: Request, proxy: (backend: string, options: ProxyOptions) => void, params: any) => void): void
  • Calls the provided callback to send a response.

    deprecated

    Parameters

    • cb: (request: Request, proxy: (backend: string, options: ProxyOptions) => void, params: any) => void

      Render callback function

        • (request: Request, proxy: (backend: string, options: ProxyOptions) => void, params: any): void
        • Parameters

          Returns void

    Returns void

send

  • send(content: string | (() => string), statusCode?: number, statusMessage?: undefined | string): void
  • Sends content back to client. If content is a string, the respons will be sent directly from the edge. If it is a function, the request will be computed by a JavaScript worker.

    Example

     new Router()
       .get('/some/path', ({ send }) => {
         return send('<html><body>Hello World!</body></html>', 200, 'OK')
       })

    Parameters

    • content: string | (() => string)

      The response body as a string

    • Default value statusCode: number = 200

      The status to send

    • Optional statusMessage: undefined | string

      The status message to send

    Returns void

serveStatic

  • serveStatic(path: string): void
  • Responds with a static asset from the specified path.

    Example

    serveStatic('path/to/asset/from/app/root')

    You can also use variables in the asset path. For example, to return files under the assets directory when the url starts with /static:

     new Router()
       .get('/static/*path', ({ serveStatic }) => {
         return serveStatic('assets/*path')
       })

    Parameters

    • path: string

      The relative path to the asset from the app's root directory. You can reference path variables using :variable.

    Returns void

    A promise the resolves once the asset has been fetched from storage.

serviceWorker

  • serviceWorker(path: string): void
  • Returns the service worker with proper edge and browser cache headers

    Parameters

    • path: string

      The path to the service worker relative to the root directory of your app

      Example

       new Router()
         .get('/service-worker.js', ({ serviceWorker }) => {
           return serviceWorker('dist/service-worker.js')
         })

    Returns void

setRequestHeader

  • setRequestHeader(name: string, value: string): void
  • Adds or replaces a request header. This method should be called before proxy.

    Example

     new Router()
       .get('/some/path', async ({ setRequestHeader, proxy }) => {
         setRequestHeader('some-header', 'some-value')
         await proxy('origin')
       })

    Parameters

    • name: string

      The case-insensitive name of the request header

    • value: string

      The value to set

    Returns void

setResponseHeader

  • setResponseHeader(name: string, value: string): void
  • Adds or replaces a response header immediately before delivering the response downstream. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ setResponseHeader, proxy }) => {
         await proxy('origin')
         setResponseHeader('some-header', 'some-value')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    • value: string

      The value to set

    Returns void

setUpstreamResponseHeader

  • setUpstreamResponseHeader(name: string, value: string): void
  • Adds or replaces an upstream response header before attempting to cache the response. In local development this is same as calling setResponseHeader but on edge it adds a header immediately after the upstream fetch has finished. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ setUpstreamResponseHeader, proxy }) => {
         await proxy('origin')
         setUpstreamResponseHeader('x-xdn-surrogate-key', 'proxy=origin')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    • value: string

      The value to set

    Returns void

updateRequestHeader

  • updateRequestHeader(name: string, match: RegExp, replace: string): void
  • Alters a request header. Use this method to derive the new header value from the existing one. This method should be called before proxy.

    Example

     new Router()
       .get('/some/path', async ({ updateRequestHeader, proxy }) => {
         updateRequestHeader('some-header', /some-.*-part/gi, 'some-replacement')
         await proxy('origin')
       })

    Parameters

    • name: string

      The case-insensitive name of the request header

    • match: RegExp

      Regex to find the part that should be replaced.

    • replace: string

      Value that will replace the matched part.

    Returns void

updateResponseHeader

  • updateResponseHeader(name: string, match: RegExp, replace: string): void
  • Alters a response header immediately before delivering the response downstream. Use this method to derive the new header value from the existing one. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ updateResponseHeader, proxy }) => {
         await proxy('origin')
         updateResponseHeader('some-header', /some-.*-part/gi, 'some-replacement')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    • match: RegExp

      Regex to find the part that should be replaced.

    • replace: string

      Value that will replace the matched part.

    Returns void

updateUpstreamResponseHeader

  • updateUpstreamResponseHeader(name: string, match: RegExp, replace: string): void
  • Alters an upstream response header before attempting to cache the response. Use this method to derive the new header value from the existing one. In local development this is same as calling updateResponseHeader but on edge it updates a header immediately after the upstream fetch has finished. This method should be called after proxy.

    Example

     new Router()
       .get('/some/path', async ({ updateUpstreamResponseHeader, proxy }) => {
         await proxy('origin')
         updateUpstreamResponseHeader('cache-control', /public/gi, 'private')
       })

    Parameters

    • name: string

      The case-insensitive name of the response header

    • match: RegExp

      Regex to find the part that should be replaced.

    • replace: string

      Value that will replace the matched part.

    Returns void

Generated using TypeDoc