Skip to content

Latest commit

 

History

History
171 lines (92 loc) · 3.9 KB

extension-api-of-the-application-router-a36f409.md

File metadata and controls

171 lines (92 loc) · 3.9 KB

Extension API of the Application Router

A detailed list of the features and functions provided by the application router extension API.

The application router extension API enables you to create new instances of the application router, manage the approuter instance, and insert middleware using the Node.js “connect” framework. This section contains detailed information about the following areas:

  • Approuter Extension API Reference

  • Middleware Slot

The application router uses the “Connect” framework for the insertion of middleware components. You can reuse all connect middleware within the application router directly.

Approuter Extension API Functions

approuter()

Creates a new instance of the application router

first

Defines a “middleware slot” (a slot for the insertion of middleware) immediately after the connect application is created, and before any application router middleware

Tip:

This is a good place to insert infrastructure logic, for example, logging and monitoring.

beforeRequestHandler

Defines a “middleware slot” before the standard application router request handling, that is; static resource serving or forwarding to destinations.

Tip:

This is a good place to handle custom REST API requests.

beforeErrorHandler

Defines a “middleware slot” before the standard application router error handling

Tip:

This is a good place to capture or customize error handling.

start(options, callback)

Starts the application router with the given options.

  • options this argument is optional. If provided, it should be an object which can have any of the following properties:

    • port - a TCP port the application router will listen to (string, optional)
    • workingDir - the working directory for the application router, should contain the xs-app.json file (string, optional)
    • extensions - an array of extensions, each one is an object as defined in Application Router Extensions (optional)
    • xsappConfig - An object representing the content which is usually put in xs-app.json file. If this property is present it will take precedence over the content of xs-app.json.
  • callback - optional function with signature callback(err). It is invoked when the application router has started or an error has occurred. If not provided and an error occurs (for example the port is busy), the application will abort.

close(callback)

Stops the application router.

  • callback - optional function with signature callback(err). It is invoked when the application router has stopped or an error has occurred.

Manage the insertion of middleware slots with the application router.

use(path, handler)

Inserts a request handling middleware in the current slot.

  • path - handle only requests starting with this path (string, optional)

  • handler - a middleware function to invoke (function, mandatory)

Returns this for chaining.

Related Information

Middleware Connect Framework

Extending the Application Router