Creates a singleton instance of toki and starts the bootstrapping process.
-
options
required At minimum, a router is required. In most cases you only need to initialize the bridge itself, which in turn will setup Toki for you. Toki accepts the following:router
A configured bridgelogger
An optional logger to make use of. Should expose log4j style methods (error
,warn
,info
,debug
,trace
)
Fired when toki is ready.
//intantiate toki
const Toki = require('toki');
toki.on('ready', ()=>{
//Ready
});
Fired for errors.
//instantiate toki
const Toki = require('toki');
toki.on('error', (error)=>{
//check error to find out what happened
});
toki subscribes to events triggered by toki-config if the underlaying configuration mechanism detects a configuration change. toki will bubble up the event.
//instantiate toki
const Toki = require('toki');
toki.on('config.changed', ()=>{
});
Fulfilled by the Bridge
These contracts supply a minimum. Additional properties or methods may exist, but should not be used since requests and responses provided by the bridge are often decorated versions of underlying requests or responses.
Request is a decorated version of the Node http-server request object. It will always have the following:
request.query
- a parsed query objectrequest.params
- an object of any params from passed pathsrequest.path
- the current pathrequest.method
- the method which called this requestrequest.headers
- an object containing all headers
Response is an object which allows you to send data back to the client as well as set status codes, headers and return errors.
response.send(payload)
where payload is a string, an object or a promise. If payload is an instance of error, it'll be sent to response.error().response.error(error)
where payload is an instance of error will send back a default status code as well as show the error.response.code(status)
where status is a number will send back that statusCode. It can be called before or after send().response.header(name, value)
will set the named header to the new value. It can be called before or after send().