@StarpTech/Hemera 5
Nodejs

Hemera 5 Demo with public NATS Server

I built🔨
THIS
Files
  • index.js
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
		});
		hemera.log.info(response.data, 'Result 1');

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

start();
node v9.7.1 linux/amd64