巢狀呼叫
const seneca = require('seneca')()
seneca.add({cmd: 'config'}, function (msg, done) {
var config = {rate: 0.23}
var value = config[msg.prop]
done(null, {value: value})
})
seneca.add({cmd: 'salestax'}, function (msg, done) {
seneca.act({cmd: 'config', prop: 'rate'}, function (err, result) {
var rate = parseFloat(result.value)
var total = msg.net * (1 + rate)
done(null, {total: total})
})
})
seneca.act({cmd: 'salestax', net: 100}, function (err, result) {
console.log(result.total)
})
Seneca 跟 Koa 整合範例
const Koa = require('koa')
const app = new Koa();
const Router = require('koa-router')
const koaBody = require('koa-body');
const SenecaWeb = require('seneca-web')
const Seneca = require('seneca')
const seneca = Seneca();
app.use(koaBody());
app.use(Router().routes());
app.use(Router().allowedMethods());
app.listen(3000);
const senecaWebConfig = {
context: Router(),
adapter: require('seneca-web-adapter-koa2')
}
seneca.use(SenecaWeb, senecaWebConfig );
seneca.use('../services/api');
seneca.client( { type:'tcp', pin:'role:math' } );
seneca.ready(() => {
app.use(seneca.export('web/context')().routes())
});