iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 24
1

巢狀呼叫

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())
});

上一篇
Seneca 介紹
系列文
Microservices in Action: with example in Node.js24
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言