Skip to content

Instantly share code, notes, and snippets.

@leo60228
Last active March 12, 2017 20:57
Show Gist options
  • Save leo60228/8be2208e82531e59c36a7a9301b94088 to your computer and use it in GitHub Desktop.
Save leo60228/8be2208e82531e59c36a7a9301b94088 to your computer and use it in GitHub Desktop.
Extension API Docs

Loading Extensions

All .js files in extensions/ are loaded and used as Extensions.

Extension

An Extension is simply a standard Node.js module that exports an ExtensionFunc.

ExtensionFunc

A function with the following properties:

ExtensionFunc.configNamespace: String?

Optional. If specified, sets the object under config.extensions that will get passed to this ExtensionFunc when called.

ExtensionFunc(config?: Object): Handler[]

Config is ​`config.extensions.${configNamespace}`​ if configNamespace is defined, or undefined otherwise. The return value is appended to an internal array of Handlers, looped through on each request.

Handler

Handler.priority: String = "normal"

early, normal, or late. When a request is received, it will go through each Handler in the order early > normal > late, with order within each priority being undefined. Files are loaded after early if a handler in early did not already respond.

Handler.matchPath: String/Handler.matchErr: Number

Exactly one of these must be defined. This Handler is used if either Handler.matchErr matches what was set for the request, with 9 being a wildcard digit, or Handler.matchPath matches the request path as a glob expression, with / being prepended if not already there.

Handler.handle(resp?: Response): String?

resp is the current Response. If it returns a string, the response's body is replaced with it.

Response

Response.errCode: Number

The error code of the response sent. Defaults to 200 on a new instance.

Response.body: String

The response sent to the client.

Response.forward(path: String?): Void

After calling this, once you return all data on this object is erased, and the request is reinterpreted as being for path. If path is undefined, only matchErr is used in the handlers ran.

Response.headers: Object

Headers sent in the response.

Response.request: Request

The matching Request for this Response.

Request

Request.path: String (read-only)

The path this request was sent to.

request.headers: Object (read-only)

Headers sent in the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment