@StarpTech/Hemera 5
Nodejs

Hemera 5 Demo with public NATS Server

Files
  • index.js
  • package-lock.json
  • package.json
index.js
const Hemera = require('nats-hemera');
const HemeraJoi = require('hemera-joi');
const nats = require('nats').connect('nats://demo.nats.io:4222');

const hemera = new Hemera(nats, {
	logLevel: 'info'
});

// set payload validator of your choice
hemera.use(HemeraJoi);

const start = async () => {
	try {
		// establish connection and bootstrap hemera
		await hemera.ready();

		// use exposed lib from plugin
		let Joi = hemera.joi;

		// define your first server action
		hemera.add(
			{
				topic: 'math',
				cmd: 'add',
				a: Joi.number().required(),
				b: Joi.number().required()
			},
			async function(req) {
				return req.a + req.b;
			}
		);
		hemera.log.info('service listening');

		// start first request
		let response = await hemera.act({
			topic: 'math',
			cmd: 'add',
			a: 10,
			b: 10
		});
		console.log('Result 1', response.data);

		// keep the parent "context" to retain meta and trace informations
		response = await response.context.act({
			topic: 'math',
			cmd: 'add',
			a: 10,
			b: 10
		});
		console.log('Result 2', response.data);
	} catch (err) {
		hemera.log.error(err);
		process.exit(1);
	}
};

start();
node v9.7.1 linux/amd64