iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
0
Software Development

Microservices in Action: with example in Node.js系列 第 23

Seneca 介紹

Seneca 是什麼?

Seneca is a microservices toolkit for Node.js. It helps you write clean, organized code that you can scale and deploy at any time.

Seneca 是一個建構在 Node.js 的微服務工具組,它能幫助你寫出有組織且乾淨的 Code,你可以在任何時間彈性的擴充跟佈署。

它提供兩大核心功能,pattern matching 和 transport independence。

你可以使用這個模組定義一些 command,它的運作方式是透過 pattern matching 的方式來執行,你可以定義你自己的 command。

安裝 Seneca

npm install seneca --save

最基本的範例

  • pattern matching:從上面的範例中看到 add 跟 act 是成對的,而 {cmd: 'salestax'} 就是它們的 match 方式。
  • add 函式有 pattern 和 action 參數
  • act 函式有 msg 和 respond 參數
const seneca = require('seneca')()
 
// 2
seneca.add({cmd: 'salestax'}, function (msg, done) {
  var rate  = 0.23
  var total = msg.net * (1 + rate)
  // 3
  done(null, {total: total})
})
 
// 1
seneca.act({cmd: 'salestax', net: 100}, function (err, result) {
  // 4
  console.log(result.total)  // output: 123
})

上一篇
不知道大家有沒有比較推薦講微服務的影片?
下一篇
Seneca 進階範例
系列文
Microservices in Action: with example in Node.js24
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言