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"
}
}
"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
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
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 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}
Comments
Post a Comment