Created
October 15, 2015 15:13
-
-
Save maxired/347f27231e9a52990f59 to your computer and use it in GitHub Desktop.
Sample for senecajs/senecajs.org/#74
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// require seneca | |
var seneca = require('seneca') | |
// create the seneca instance | |
var si = seneca({log: 'silent'}) | |
// add a worker to parse string | |
.add('command:parse,type:string', function (args, done) { | |
done(null, JSON.parse(args.value)) | |
}) | |
// create a function who send a parse command, | |
// with type and content as parameter | |
// if no service match the type, the content is returned | |
var parseOrReturnValue = function (type, content, cb) { | |
si.act('command:parse', {type: type, value: content, default$: content}, cb) | |
} | |
// request with type string : it will be parsed by the service | |
parseOrReturnValue('string', '{"index":"foo","value":42}', function (err, res) { | |
console.log('res string\t', err, res.index, res.value) | |
}) | |
// request with type obj : the obj will be returned as it | |
parseOrReturnValue('obj', {index: 'bar', value: 404}, function (err, res) { | |
console.log('res obj\t\t', err, res.index, res.value) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment