Skip to main content

Seneca

Message based Microservices on Node.JS

Seneca is a microservices toolkit for Node.js. Seneca lets you build message based microservice systems with ease. 

This blog is for beginners to start off with Seneca.
Here is what you need to do:
  • Install NPM on your machine
  • Create a folder on your machine example "D:\\FirstSenecaProgram"
  • Create a new file name it "package.json", keep in mind the extension of the file should be *.json
    Place the below content in to "package.json" file
{
  "name": "SenecaMicroservices",
  "version": "1.0.0",
  "description": "Code examples for the Seneca",
  "author": "",
  "license": "",
  "dependencies": {
    "body-parser": "^1.13.2",
    "express": "^4.13.1",
    "seneca": "2.0.0",
    "seneca-entity": "0.0.1",
    "seneca-web": "^2.0.0",
    "seneca-web-adapter-express": "^1.0.2"
  }
}

All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.
  • Now open command prompt, run as Administrator.
  • Go to directory "cd D:\\FirstSenecaProgram"
  • Execute command "npm install", which would install the required project artifacts defined in "package.json"
    Note: Wait for some time till the artifacts are generated.
  • Create a new file name it "math.js", keep in mind the extension of the file should be *.js
    Place the below content in to "math.js" file
module.exports = function math( options ) { 
  this.add( 'role:math,cmd:addition', function addition( msg, respond ) {
    respond( null, { answer: msg.left + msg.right } )
  })
  this.add( 'role:math,cmd:subtraction', function subtraction( msg, respond ) {
    respond( null, { answer: msg.left - msg.right } )
  })
  
  this.add( 'role:math,cmd:multiplication', function multiplication( msg, respond ) {
    respond( null, { answer: msg.left * msg.right } )
  })
  
  this.add( 'role:math,cmd:division', function division( msg, respond ) {
    respond( null, { answer: msg.left / msg.right } )
  })
  this.wrap( 'role:math', function( msg, respond ) {
    msg.left  = Number(msg.left).valueOf()
    msg.right = Number(msg.right).valueOf()
    this.prior( msg, respond )
  })
}
  • Create a new file name it "api.js", keep in mind the extension of the file should be *.js
    Place the below content in to "api.js" file
module.exports = function api(options) {
  var valid_ops = { addition:'addition', subtraction:'subtraction', multiplication:'multiplication', division:'division' }
  this.add('role:api,path:calculate', function (msg, respond) {
    var operation = msg.args.params.operation
    var left = msg.args.query.left
    var right = msg.args.query.right
    this.act('role:math', {
      cmd:   valid_ops[operation],
      left:  left,
      right: right,
    }, respond)
  })
  this.add('init:api', function (msg, respond) {
    this.act('role:web',{routes:{
      prefix: '/api',
      pin:    'role:api,path:*',
      map: {
        calculate: { GET:true, suffix:'/:operation' }
      }
    }}, respond)
  })
}
  • Create a new file name it "app.js", keep in mind the extension of the file should be *.js
    Place the below content in to "app.js" file
var SenecaWeb = require('seneca-web')
var Express = require('express')
var Router = Express.Router
var context = new Router()
var senecaWebConfig = {
   context: context,
   adapter: require('seneca-web-adapter-express'),
   options: { parseBody: false }
}
var app = Express()
   .use( require('body-parser').json() )
   .use( context )
   .listen(3000)
var senecaClient = require('seneca')()
   .use(SenecaWeb, senecaWebConfig )
   .use('api')
   .client( { type:'tcp', pin:'role:math' } )
\t 
var senecaListen= require( 'seneca' )()
   .use( 'math' )
   .listen( { type:'tcp', pin:'role:math' } )

----------------------------------------------------------------------------------------
Don't worry, no more coding...
----------------------------------------------------------------------------------------

After creating the required 4 files (package.json, math.js, api.js, & app.js) in folder "D:\\FirstSenecaProgram"
  • Now open command prompt, run as Administrator.
  • Go to directory "cd D:\\FirstSenecaProgram"
  • Execute command "nodemon app.js", this would start up the server.
Open your browser and invoke the following url's and I'm providing the expected output:
  • http://localhost:3000/api/calculate/addition?left=3&right=2
  • {"answer":5}
    ------------------------------------------------------------------------------------
  • http://localhost:3000/api/calculate/subtraction?left=3&right=2
  • {"answer":1}
    ------------------------------------------------------------------------------------
  • http://localhost:3000/api/calculate/multiplication?left=3&right=2
  • {"answer":6}
    ------------------------------------------------------------------------------------
  • http://localhost:3000/api/calculate/division?left=3&right=2
  • {"answer":1.5}
Learn more @ senecajs.org, this is just to get you started.

Comments

Popular posts from this blog

Everything about Java 8

The following post is a comprehensive summary of the developer-facing changes coming in Java 8. This next iteration of the JDK is currently scheduled for general availability in  September 2013 . Read More

Hands-on with Mozilla’s Web-based “Firefox OS” for smartphones

Launching a new mobile OS is a difficult project since the market leaders, Android and iOS, have such  a big lead. Even Microsoft, with its near-infinite financial resources and vast ecosystem of complementary products, has struggled to gain traction. And new entrants face a chicken-and-egg problem: developers don't want to write apps for a platform without many users, while users don't want to buy a phone without many apps. Mozilla, the non-profit foundation behind Firefox, believes it can tackle this dilemma. In 2011, it announced a new project  called Boot2Gecko to build an operating system around its browser. Last year the project was  re-branded Firefox OS, and Mozilla began preparations for a major push into the mobile phone market. In February, Mozilla  unveiled an impressive initial list  of hardware and network partners. If all goes according to plan, Firefox OS phones will be available in a number of countries, mostly in the developing world, la...

Three reasons Microsoft wants to kill the Windows Desktop

Microsoft's Windows Blue update to Windows 8  makes it increasingly clear that Microsoft wants to kill the Desktop.  That may seem self-defeating, but there's method in Microsoft's madness. Here are three reasons I think it wants to eventually kill the Desktop. Help Windows Phone and Windows tablets gain market share Unify the operating system Lock enterprises into future versions of Windows Read More