Mocks Server provides the
mocksServer instance to plugins, middlewares and other system pieces. The
mocksServer instance is the Mocks Server core itself, and it contains methods allowing to configure, start or stop it, getters returning information, etc.
You could also create your own
mocksServer instance programmatically. Read the programmatic usage chapter for further info.
init([programmaticOptions]): Register plugins, initialize options and prepare all other internal dependencies needed to start the server. Returns a promise. Accepts next arguments:
start(): Start the server and plugins. Returns a promise.
stop(): Stop the server and plugins. Returns a promise.
restartServer(): Restart the server.
onChangeMocks(callback): Add a callback to be executed when mocks or routes changes. Returns a function for removing the added callback.
callback()(Function): Function to be executed on change mocks.
onChangeSettings(callback): Add a callback to be executed when settings are changed. Returns a function for removing the added callback.
callback([changedSettings])(Function): Function to be executed on change settings.
changedSettings(Object): Settings properties that have changed, with new values.
onChangeAlerts(callback): Add a callback to be executed when alerts change. Returns a function for removing the added callback.
callback([currentAlerts])(Function): Function to be executed on change alerts.
currentAlerts(Array): Current alerts.
addSetting(customSetting): Register a new setting (which will be available also as an
optionduring initialization). Has to be called before the
mocksServer.initmethod is called. (It should be usually used by Plugins in their
customSetting(Object): containing next properties:
name(String): Name of the new option.
type(String): One of
boolean. Defines the type of the new option.
description(String): Used for giving help to the user in command line arguments, for example.
default(Any): Default value for the new option.
parse(Function) Custom parser for the option when it is defined using command line arguments.
addRouter(path, expressRouter): Add a custom express router to the server. Custom routers will be added just before the middleware that serves the routes, so if a custom router path matches with a route path, the first one will have priority.
path(String): Api path for the custom router
expressRouter(Express Router): Instance of an
removeRouter(path, expressRouter): Remove a custom express router previously added with the
path(String): Api path of the custom router to be removed.
expressRouter(Express Router): Instance of the express router to be removed.
addRoutesHandler(RoutesHandler): Add a custom routes handler. This allows to add new formats of defining route variants.
<Class>Custom routes handler. Read the adding custom route handlers chapter for further info.
tracer: Contains methods for using the built-in Mocks Server formatted tracer. Depending of the current
logsetting, the message will be printed or not:
set(level, [transport]): Sets the tracer current log level.
level(String): Level can be one of
transport(String): The tracer transport in which the level has to be set. Can be one of
store. Default is
store: Returns an array with logs (only last 1000 are stored). The level of logs stored can be changed using
deprecationWarn(oldMethodName, newMethodName)Call to
tracer.warnfor giving feedback about a method that is going to be deprecated.
settings: Contains methods for interacting with the Mocks Server settings. Settings are equivalent to
options, but another name is used because they are already initialized and contains definitive values, taking into account command line arguments or other configuration methods. Available methods are:
set(key, value): Changes the value of a setting.
key(String): The name of the setting to be changed. Equivalent to option name.
value(Any): New value for the specific setting to be set.
get(key): Returns current value of an specific setting.
key(String): The name of the setting to be returned. Equivalent to option name.
all: Getter returning all current settings. Never modify returned object if you want to change settings, as it will have no effect. Use the
getValidOptionName(optionName): Returns valid option name if it exists, or new option name if it is deprecated but is still supported, and
nullif it does not exist.
optionName(String): Option name to check.
lowLevelConfig: Returns current low level configuration properties.
mocks: Returns methods and getters related to currently available mocks and routes.
useRouteVariant(variantId): Define a route variant to be used by the current mock. Route variants are restored whenever the current mock changes.
variantId(String): Route variant id, with the format
restoreRoutesVariants(): Restore current mock route variants. It removes all variants defined with the
customRoutesVariants: Getter returning array of currently used routes variants ids.
current: Getter returning current
ids: Getter returning an array with all mocks ids.
plainMocks: Returns an array with current mocks in plain format.
plainRoutes: Returns an array with current routes in plain format.
plainRoutesVariants: Returns an array with current routes variants in plain format.
alerts: Returns an array of current alerts.