Methods

This handler type acts mostly like the well known request-response pattern. Define methods on the server and execute them from the client. Return values will be send back to client.

// Define a method which returns current date
server.method('getCurrentTime', () => Date.now());

// Execute defined method from client and log the result
client.exec('getCurrentTime').then(time => console.log(time));

Method-handlers could also return promises (client.exec() will always return a promise):

// Define a method which returns a promise
server.method('getAsyncTime', () => Promise.resolve(Date.now()));

// Execute defined method from client and log the result
client.exec('getCurrentTime').then(time => console.log(time));

Like every handler also method-handlers execute defined preHandler features:

server.method('getSecretTime', {
  auth: {
    schema: 'myAuthSchema',
    scope: ['admin']
  },
  handler: () => Promise.resolve(Date.now)
});

results matching ""

    No results matching ""