# new Hook()
This is the HTTP Hook Singleton. It does not need to be instantiated, so you can safely ignore the constructor. You can access methods on it in this way - "interface.hook.method". This class contains methods that allow you to hook in to HTTP requests and responses, and execute intermediate functions on the content of these messages.
- Source:
const hook = o.hook;
Methods
# (static) add(names, hookType, hookFn) → {Promise}
This method (Add Hook) allows apps to register a hook function that intercepts the HTTP messages at one of four supported stages (as defined by the hookType parameter). The possible values for the hookType parameter, each representing a distinct stage of the HTTP handling lifecycle, are - onIncomingRequest, onOutgoingResponsePostRender, onOutgoingRequest and onIncomingResponse.
Name | Type | Description |
---|---|---|
names |
string | array | Array or String Containing Interface Name(s) |
hookType |
string | Type of Hook |
hookFn |
function | Hook Function |
- Source:
promise - Promise
- Type
- Promise
const http = core.module('http', 'interface');
http.hook.add('*', 'onOutgoingResponsePostRender', function(input, cb) {
const output = '<h1>THIS HTML WILL BE INJECTED INTO THE TOP OF EVERY PAGE' + input;
cb(output);
}).then(function(addResult) {
const hookId = addResult.hookId
console.log(hookId);
// OUTPUT: "12345-1234-12345-1234"
});
# (static) remove(hookId) → {boolean}
This method (the Remove Hook Method) does exactly what its name implies. It removes a hook that was added via the Add Hook method from the HTTP processing pipelines, so that the message is no longer intercepted by THAT particular hook function. Of courses, multiple hooks may have been added, and therefore others may remain.
Name | Type | Description |
---|---|---|
hookId |
string | The Hook UUID to Remove |
- Source:
result - True | False
- Type
- boolean
const http = core.module('http', 'interface');
http.hook.remove('12345-1234-12345-1234');