Skip to content

Instantly share code, notes, and snippets.

@StarpTech StarpTech/hemera-hapi.js
Last active Apr 22, 2017

Embed
What would you like to do?
Hemera with Hapi
'use strict'
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({ port: 80 })
server.register({
register: require('hapi-hemera'),
options: {
hemera: {
name: 'test',
load: {
sampleInterval: 1
},
logLevel: 'info'
},
nats: 'nats://localhost:4222',
plugins: [
require('hemera-joi'),
require('hemera-stats')
]
}
}).then(() => {
return server.start()
}).then(() => {
console.log(`Server running at: ${server.info.uri}`);
// hemera can be used when the plugin was initialized
let Joi = server.hemera.exposition['hemera-joi'].joi
server.hemera.add({
topic: 'math',
cmd: 'add',
a: Joi.number().required(),
b: Joi.number().required()
}, function (req, cb) {
cb(null, parseInt(req.a) + parseInt(req.b))
})
server.route({
method: 'GET',
path: '/api/add',
handler: function (request, reply) {
request.hemera.act({
topic: 'math',
cmd: 'add',
a: request.query.a,
b: request.query.b,
refresh: !!request.query.refresh
}, function (err, result) {
if (err) {
return reply(Boom.wrap(err.cause, 400))
}
return reply(result)
})
}
})
})
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.