Skip to content

6. Events

{Ed}uardo Veras edited this page Jul 7, 2022 · 3 revisions

The events listed on the request lifecycle can be binded on the server level, using the method on(criteria, listener) as:

const server = new slsRouter.Server({ Joi : require('joi') });

server.on('onEvent', async event => { //v1.0.7+
  console.log('onEvent', event);
});

server.on('onRoute', async (route, event) => { //v1.3.0+
  //In case a route can't be found for the request path, the "route" will be "null"
  console.log('onRoute', route, event);
});

server.on('onRequest', async request => { //v1.0.7+
  console.log('onRequest', request);
});

server.on('onPostAuth', async request => { //v1.0.7+
  console.log('onPostAuth',request);
});

server.on('onPreHandler', async request => { //v1.0.7+
  console.log('onPreHandler', request);
});

server.on('onPostHandler', async payload => { //v1.0.7+
  console.log('onPostHandler', payload);
});

server.on('onPreResponse', async (response, event) => { //v1.0.7+
  console.log('onPreResponse', event);
  console.log('onPreResponse', response);
  /*
  response.error <default:null> v1.0.15+
  In case the script fails, this field returns the raw error object.
  Good to create custom error messages on the response.
  */
  if(response.error){
    console.error(response.error);
  }
});
Clone this wiki locally