App

Extends

appManager
readonly
Type:
AppManager
config
readonly

see affiliated serviser-config npm package

Type:
Config
name
readonly
Type:
String
resourceManager
readonly
Router
readonly

App specific Router constructor

Type:
function
service
readonly
Type:
Service
status
readonly

one of AppStatus enum

Type:
String
new App(appManager, config, options)

App is http impementation of AppInterface and represents a bundle of Routers with Routes. It holds http[s] server object or its equivalent/replacement and references to the AppManager and Service instances which it was created from. It also manages its own Config instance with restricted scope

Parameters:
  • config
    • Type: Config
    • module

  • options
    • Type: Object
      • name
        • Type: String
        • app's name

      • validator optional
        • Type: Object
        • Ajv validator initialization options

          • schemas optional
            • Type: Object or Array
            • list of globally accessible schema definitions

Fires:
getHost()String

returns protocol + host url string

Returns: String
listen(port, hostname, backlog, options)

start http(s) server listening on configured port

Parameters:
  • port
    • Type: Integer or String
    • or socket

  • hostname optional
    • Type: String
  • backlog optional
    • Type: Integer
    • the maximum length of the queue of pending connections. The actual length will be determined by your OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on linux. The default value of this parameter is 511 (not 512).

  • options optional
    • Type: Object
      • ssl optional
        • Type: Boolean
        • Default: false
Returns: http[s].Server
use(endpoint, callback)undefined

bind application-level middleware that will be run before Route's middleware stack

Parameters:
  • endpoint optional
    • Type: String
  • callback optional
    • Type: function
Returns: undefined
buildRouter(options)Router
Parameters:
  • options
    • Type: Object
      • public optional
        • Type: String
      • version optional
        • Type: String
      • url
        • Type: String
Returns: Router
close()Promise

shutdown underlying net socket. if not running, resolved Promise will be returned

Returns: Promise
getRoute(uid)Route
Parameters:
  • uid
    • Type: String
Throws:
  • Error - when route is not found

Returns: Route
getValidator()Ajv
Returns: Ajv validator instance
on(event, callback)Boolean

registeres event listener.
overrides event emmiter implementation

Parameters:
  • event
    • Type: String
  • callback
    • Type: function
Returns: Boolean
build-router

emitted with each AppInterface#buildRouter method call.

Properties:
error

fires each time an unexpected internal Error is encoutered. When the Error is catched in the application space, the Error is coerced to ServiceError type which is safe to respond with.

Internal listener is binded at initialization time which logs all received Errors with the serviser-logger package.
AppInterface#status is updated with the first internal error setting the Application to error state.

Properties:
  • error
    • Type: Error
error-response

By default an App handles all "expected" & unexpected Errors automatically and responds to a request accordingly.
By pushing a listener to this event, you can define custom user error processing logic and respond to the request manually.
Listeners of this event are executed asynchronously - Promises are supported.

Properties:
  • res
    • Type: http.ServerResponse
    • response

Example:
app.on('error-response', function(err, res) {
    //pseudocode:
    //renders html view and sends html response instead of default json response
    return res.render('error', err); //returns a Promise
});
listening

reflects http[s] server listening event

Properties:
post-build

emitted after app route definitions are assembled into a single function and binded to internal http[s] server.

Properties:
post-init

emitted after internal initialization of the App instance and before AppInterface#event:pre-build and AppInterface#event:post-build events.
This is the time to register application wide middlewares which get executed before the main route implementation.

Properties:
pre-build

emitted before app route definitions are assembled into a single function.

Properties:
pre-init

emitted before internal initialization of the App instance which will validate configuration, setup default application wide response headers and error handling middlewares

Properties:
status-changed

emitted once each time after status change. Once you get AppStatus#ERROR status, the App's status can NOT be changed thus no more status-changed events will be emitted.

Properties:
  • status
    • Type: String
    • see AppStatus enum for available option values

unknown-error

Is emitted before a response to a request is sent and allows to convert an unknown error (an error which is not instanceof RequestError and at the same time is not dirrect instanceof Error) to RequestError which will be then processed in place of the original unknown error object.
if no listener is present all unknown errors will be automatically converted to ServiceError

Properties:
  • err
    • Type: Error
  • errorHandler
    • Type: function
    • callback function

Example:
app.on('unknown-error', function(err, errorHandler) {
    if (err instanceof SequelizeUniqueConstraintError) {
        return errorHandler(new RequestError('Entity already exists'));
    }
    //hand back the error processing to the application
    return errorHandler(err);
});
comments powered by Disqus