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

Google leaps language barrier with translator phone

GOOGLE is developing software for the first phone capable of translating foreign languages almost instantly — like the Babel Fish in The Hitchhiker’s Guide to the Galaxy. By building on existing technologies in voice recognition and automatic translation, Google hopes to have a basic system ready within a couple of years. If it works, it could eventually transform communication among speakers of the world’s 6,000-plus languages. The company has already created an automatic system for translating text on computers, which is being honed by scanning millions of multi-lingual websites and documents. So far it covers 52 languages, adding Haitian Creole last week. Google also has a voice recognition system that enables phone users to conduct web searches by speaking commands into their phones rather than typing them in. Now it is working on combining the two technologies to produce software capable of understanding a caller’s voice and translating it into a synthetic equivalent in a foreign ...

Keyboard First Impressions: Kinesis Advantage Mechanical Ergonomic Keyboard

An ergonomic keyboard with mechanical switches that’s looking to attract users interesting in a high quality, highly ergonomic offering and don’t mind the rather steep learning curve or the price. The  TECK  isn’t the only such keyboard, of course, and I decided to see what other mechanical switch ergonomic keyboards I could get for comparison. Next up on the list is the granddaddy of high-end ergonomic keyboards, the  Kinesis Contour Advantage . I wanted to provide my first impressions of the Kinesis, along with some thoughts on the initial switch and the learning curve. This time, I also made the effort to put together a video of my first few minutes of typing. It actually wasn’t as bad as with the TECK, but that’s likely due to the fact that I already lost many of my typing conventions when I made that switch earlier this year. I’ll start with the video, where I take a typing test on four different keyboards and provide some thoughts on the experience, and then ...