- Status: In development
hapi-hemera is a Hemera micro-services plugin for hapi. The plugin integrates the Hemera functionality into hapi and provide tools to map its actions to server methods and views for easy access.
const server = new Hapi.Server()
server.connection()
server.register({
register: require('hapi-hemera'),
options: {
hemera:{
logLevel: 'info'
},
nats: 'nats://localhost:6242',
// In case you want to use hapi server methods
methods: {
add: {
pattern: {
topic: 'math',
cmd: 'add',
timeout$: 5000,
}
// Optional caching parameters
cache: {
expiresIn: 60000,
staleIn: 30000,
staleTimeout: 10000,
generateTimeout: 100
}
}
},
// In case you want to use hemera plugins
plugins: [
{
name: 'hemera-joi',
onReady: {
setOption: 'payloadValidator' // execute setOption method on ready using the value `payloadValidator` as option name and the plugin name as the value
}
}
]
}
})
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
return reply.act({ topic: 'math', cmd: 'add', a: 2, b: 2 })
}
}
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
return reply.hemera.act({ topic: 'math', cmd: 'add', a: 2, b: 2 },
function(err, result) {
reply(err || result)
})
}
}
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
server.methods.add({ a: 1, b: 2 }, (err, resp) => {
reply(err || result)
})
}
}
Use body
server.route({
method: 'POST',
path: '/foo/{topic}/{cmd}',
handler: {
act: {
pattern: {
timeout$: 5000
}
}
}
})
curl -H "Content-Type: application/json" -X POST -d '{"a":2,"b":2}' http://localhost:3000/foo/math/add
use query parameters
server.route({
method: 'POST',
path: '/math/{cmd}',
config: {
validate: {
query: {
a: Joi.number().required(),
b: Joi.number().required()
}
}
},
handler: {
act: {
pattern: {
topic: 'math',
timeout$: 5000
}
}
}
})
curl http://localhost:3000/foo/math/add?a=2&b=2