Http route mapping for Hemera microservices.
npm install hemera-webHttp route mapping for Hemera microservices. Based on Express 4
* Depends on minimalist and new web framework Express 4
* Respect Body and Query payload as pattern
* Provide a REST like interface /:topic/:cmd to Hemera
* Transport small binary or text data in pattern
* Returns correct Hemera errors without stack traces


``js`
const hemera = new Hemera(nats)
hemera.use(require('hemera-web'), {
port: 3000,
host: '127.0.0.1',
pattern: {} // fixed pattern or function (request) => { }
})
You can transport the pattern in different ways:
* As Query parameters
* As Payload from type JSON
* As Payload from type x-www-form-urlencoded
* topic and cmd can be declared in url parameters
* GET Request
``
http://localhost:3000?topic=math&cmd=add&a=1&b=2
http://localhost:3000/math/add?a=1&b=2
* POST Request
`
http://localhost:3000?topic=math&cmd=add
http://localhost:3000/math/add
Body:
{
"a": 1,
"b": 2
}
`
* application/x-www-form-urlencoded
`
http://localhost:3000?topic=math&cmd=add
http://localhost:3000/math/add
Payload: a=1&bd=2
`
In Hemera:
`js
const hemera = new Hemera(nats)
hemera.use(hemeraWeb, {
port: 3000,
host: '127.0.0.1',
pattern: {
topic: 'math'
}
})
GET - http://localhost:3000?cmd=add&a=1&b=2
`
In Hemera:
`js
const CustomError = hemera.createError('CustomError')
hemera.add(
{
topic: 'math',
cmd: 'add'
},
function(req, cb) {
const error = new CustomError()
error.statusCode = 404
cb(error)
}
)
`
_Status Code_: 404 - _default (500)_
`json`
{
"error": {
"name": "Error",
"message": "test",
"hops": [
{
"service": "math",
"method": "a:1,b:2,cmd:add,topic:math",
"app": "hemera-starptech",
"ts": 299208574491
}
]
}
}
In Hemera:
`js`
const hemera = new Hemera(nats)
hemera.use(hemeraWeb, {
port: 3000,
host: '127.0.0.1',
errors: { propBlacklist: [] }
})
`js``
const hemera = new Hemera(nats)
hemera.use(hemeraWeb, {
port: 3000,
host: '127.0.0.1'
})
hemera.ready(() => {
const app = hemera.express
// Define Auth layer ... use it as always
app.use(passport.session())
})
* .express