Handlers

Handlers define the interface of your application. Mostly known as routes from HTTP-applications Durga gives you much more options and types of handlers.

Context

Handlers are executed with a single parameter only - the context. It's a simple object created for each handler-execution holding information and services.

server.method('getPayloadBack', (ctx) => {
  return ctx.payload;    
});

client.exec('getPayloadBack', { name: 'Foo' })
.then(res => {
  expect(res).to.equal({ name: 'Foo' });
});

Default context attributes

Which attributes the handler-context provides depends mostly on the handler-type but there are some default attributes which will be set on every handler-context:

  • connection holds the clients connection instance
  • handler current matched handler configuration
  • event raw message (as json-object) sent from client which caused handler execution

Thanks to ES6 destructuring assignment the previous server side code could also be written in a neat way:

server.method('getPayloadBack', ({ payload }) => {
  return payload;
});

Defining/Configure

There are two ways how to define a handler. If it should not execute any validation/autorization preHandlers you could define it the short way:

server.event('shout-out', ({ payload }) => console.log(payload.say));

If payload (e.g.) should be validated before executing handler-function you should define the handler using an object:

server.event('shout-out', {
  validate: {
    payload: {
      say: Joi.string().min(3).max(256).required()
    }
  },
  handler: ({ payload }) => console.log(payload.say)
});

preHandlers

results matching ""

    No results matching ""