5.0.2 • Published 5 years ago

hemera-web v5.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Hemera-web

Http 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

npm styled with prettier

Usage

const hemera = new Hemera(nats)
hemera.use(require('hemera-web'), {
  port: 3000,
  host: '127.0.0.1',
  pattern: {} // fixed pattern or function (request) => { }
})

How to execute a server method ?

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

Examples

  • 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

Define default pattern

In Hemera:

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

Error handling

In Hemera:

const CustomError = hemera.createError('CustomError')

hemera.add(
  {
    topic: 'math',
    cmd: 'add'
  },
  function(req, cb) {
    const error = new CustomError()
    error.statusCode = 404
    cb(error)
  }
)

Results in

Status Code: 404 - default (500)

{
  "error": {
    "name": "Error",
    "message": "test",
    "hops": [
      {
        "service": "math",
        "method": "a:1,b:2,cmd:add,topic:math",
        "app": "hemera-starptech",
        "ts": 299208574491
      }
    ]
  }
}

Show error stack for debugging

In Hemera:

const hemera = new Hemera(nats)
hemera.use(hemeraWeb, {
  port: 3000,
  host: '127.0.0.1',
  errors: { propBlacklist: [] }
})

Access to express web framework

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())
})

Plugin decorators

  • .express
5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago