nsi-routes
Node.js Services Integration - Routes commons
This project proposes a convention for asynchronous functions used to connect services together.
Using this convention we can build wrappers of functions that will allow us to do some tracing and profiling accross all routes with very little code footprints.
This project also comes with a number of predefined functions or routes, that are here purely as shortcuts to improve productivity. They also do a few things like watching resources changes to make your life easier.
Install
npm install nsi-routes
Basic usage
Initialize a routes helper with default options.
var nsiRoutes = require('nsi-routes');
var routesHelper = nsiRoutes();
Optionaly you can use an options object as parameter.
This object will be merged with the default options :
{
before: null,
after: null,
watch: false,
log: {
active: true,
config: {
console: {
level: 'info',
colorize: 'true',
label: 'nsi.routes'
}
},
metadata: {}
},
monitor: {
active: true,
config: {
console: {
level: 'info',
colorize: 'true',
label: 'nsi.monitor'
},
elasticsearch: {
level: 'error',
indexName: 'nsi-routes',
source: 'NSI - Routes',
disable_fields: true,
typeName: 'log'
}
},
metadata: {}
}
}
Wrap a route to allow profiling, tracing, etc. First parameter of wrap() is the function, second parameter is a name.
The wrapping also implicitly render the headers parameter optional, if not defined it will be replaced by an empty object when sent to the original function.
route = routesHelper.wrap(function(body, headers, callback) {
callback(null, 'response', headers);
}, 'Route 1');
route('body', function(err, responseBody, responseHeaders){
});
Advanced usage
For recipes with more advanced usage of this module, have a look at its parent project NSI.
Continuation identifier and headers
Headers are optional. If present they will be cloned so that no interactions are possible between routes.
If not present the wrapper will create a 'continuationId' headers containing a GUID.
This is very useful for logging but can also be used by the routes to identify parts of a same operation.
Common routes
NSI Queues wrappers
Simple wrappers for the already simple message sending functions from NSI - Queues helpers.
var route = routesHelper.to(queuesHelper, 'my.queue');
route('body', function(err){
if (err) console.log('Message sending failed.');
else console.log('Message was sent and acknowledged !');
});
var route = routesHelper.inOut(queuesHelper, 'my.queue');
route('body', function(err){
if (err) console.log('Message sending failed.');
else console.log('Response received: ' + body);
});
JSON schema validation
Validate messages, either text or object using a JSON schema loaded from a file and reloaded if the file changes. Uses JaySchema.
route = routesHelper.jsonSchema('test/resources/schema1.json');
route('{"id":1, "name":"name1"}', function(errs, responseBody, responseHeaders) {
});
XSD validation
Validate XML messages using a XML Schema Definition loaded from a file and reloaded if the file changes. Uses libxmljs.
route = routesHelper.xsd('test/resources/schema1.xsd');
route('<?xml version="1.0"?><product><id>1</id><name>name1</name></product>', function(errs, responseBody, responseHeaders) {
});
Handlebars templating
Transform your messages using the simple templating engine Handlebars.
route = routesHelper.handlebars('test/resources/template1.hbs');
route('my message body', function(err, responseBody, responseHeaders) {
});
The body and headers will be available in the template. For example:
'This is a template. My body: {{body}}, and a header: {{headers.myHeader}}.'
XSL transformation
Transform your messages using XSL transformations.
route = routesHelper.xslt('test/resources/stylesheet.xsl');
route(myXmlMessage, headers, function(err, responseBody, responseHeaders) {
});
HTTP requests
Send any kind of HTTP requests, using request.
The url used to initialize the route is a Handlebars template that will be rendered
at each execution of the route using the current body and headers.
If the body of the message is a javascript object, then the content will be sent as JSON and the response parsed as well.
route = routesHelper.http('GET', 'http://localhost:9615/test/{{body}}?q={{headers.query}}');
route('my message body', function(err, responseBody, responseHeaders) {
});