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

Apple's iPhone 4.0 software to deliver multitasking support

Apple this summer will go a long way towards silencing critics and catering to one of the most prevalent demands of its iPhone user base, when it introduces a multitasking solution through the handset's 4.0 software update that will finally allow several third party apps to run concurrently and in the background. For more Info.

768-bit RSA cracked, 1024-bit safe (for now)

With the increasing computing power available to even casual users, the security-conscious have had to move on to increasingly robust encryption, lest they find their information vulnerable to brute-force attacks. The latest milestone to fall is 768-bit RSA; in a paper posted on a cryptography preprint server, academic researchers have now announced that they factored one of these keys in early December. Most modern cryptography relies on single large numbers that are the product of two primes. If you know the numbers, it's relatively easy to encrypt and decrypt data; if you don't, finding the numbers by brute force is a big computational challenge. But this challenge gets easier every year as processor speed and efficiency increase, making "secure" a bit of a moving target. The paper describes how the process was done with commodity hardware, albeit lots of it. Their first step involved sieving, or identifying appropriate integers; that took the equivalent of 1,500 y...

20 Things We Already Know About Apple’s iTablet