Interface API

API publishes an internet-facing HTTP API, for serving web applications or REST APIs.

let api = new API("myapi")
api.get("/", (req, res) => res.json({hello: "world"}));
api.publish().url.then(url =>
console.log(`Serving myapi at ${url}`)
);

Paths are / seperated. A path can use {param} to capture zero-or-more non-/ characters and make the captured path segment available in req.params.param, or {param+} to greedily capture all remaining characters in the url path into req.params.param.

Paths and routing are defined statically, and cannot overlap. Code inside a route handler can be used to provide dynamic decisions about sub-routing within a static path.

interface API {
    all(path, ...handlers): void;
    attachCustomDomain(domain): void;
    delete(path, ...handlers): void;
    get(path, ...handlers): void;
    options(path, ...handlers): void;
    post(path, ...handlers): void;
    proxy(path, target): void;
    publish(): HttpDeployment;
    put(path, ...handlers): void;
    route(method, path, ...handlers): void;
    static(path, localPath, options?): void;
}

Methods

  • Routes all HTTP methods on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • Attach a custom domain to this API.

    Provide a domain name you own, along with SSL certificates from a certificate authority (e.g. LetsEncrypt).

    Must be called prior to [publish]ing the API.

    Note: It is strongly encouraged to store certificates in config variables and not in source code.

    Parameters

    Returns void

  • Routes DELETE requests on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • Routes GET requests on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • Routes OPTIONS requests on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • Routes POST requests on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • proxy forwards an HTTP request to a target URL or Endpoint.

    Parameters

    • path: string

      The route path at which to serve the file.

    • target: string | Output<Endpoint>

      The target URL or Endpoint to proxy to. If a string is provided, it must be an Internet reachable URL. If an Endpoint is provided, it can be any endpoint exposed by the stack, including endpoints which are not exposed directly to the Internet.

    Returns void

  • Publishes an API to be internet accessible.

    This should be called after describing desired routes and domains. Throws an error if called multiple times on the same endpoint.

    Returns HttpDeployment

    An HttpDeployment object representing the live API.

  • Routes PUT requests on the given path to the provided handler(s).

    Parameters

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • Routes any requests with given HTTP method on the given path to the provided handler(s).

    Parameters

    • method: string

      The HTTP method to handle.

    • path: string

      The path to handle requests on.

    • Rest ...handlers: RouteHandler[]

      One or more handlers to apply to requests.

    Returns void

  • static serves a file or directory from within the source folder at the requested path.

    Parameters

    • path: string

      The route path at which to serve the file.

    • localPath: string

      The local path. If not absolute, it is considered relative to the Pulumi program folder.

    • Optional options: ServeStaticOptions

      Optional options that can be provided to customize the serving behavior.

    Returns void

Generated using TypeDoc