本文整理汇总了TypeScript中hapi.Server.method方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.method方法的具体用法?TypeScript Server.method怎么用?TypeScript Server.method使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hapi.Server
的用法示例。
在下文中一共展示了Server.method方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
server.register({}, { select: 'api', routes: { prefix: '/prefix' } }, function (err) {});
//server.pack.register(plugin, (err: Object) => {
// if (err) { throw err; }
//});
//server.pack.register([plugin], (err: Object) => {
// if (err) { throw err; }
//});
// Add server method
var add = function (a: number, b: number, next: (err: any, result?: any, ttl?: number) => void) {
next(null, a + b);
};
server.method("sum", add);//, { cache: { expiresIn: 2000 } });
server.methods["sum"](4, 5, (err: any, result: any) => {
console.log(result);
});
var addArray = function (array: Array<number>, next: (err: any, result?: any, ttl?: number) => void) {
var sum: number = 0;
array.forEach((item: number) => {
sum += item;
});
next(null, sum);
};
server.method("sumObj", addArray, {
//cache: { expiresIn: 2000 },
示例2: function
// From https://hapijs.com/api/16.1.1#servermethodname-method-options
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
// Simple arguments
const add = function (a: number, b: number, next: Hapi.ServerMethodNext) {
return next(null, a + b);
};
server.method('sum', add, { cache: { expiresIn: 2000, generateTimeout: 100 } });
var next: Hapi.ServerMethodNext = (err, result) => {
console.log(result);
};
server.methods.sum(4, 5, next);
// Object argument
const addArray = function (array: number[], next: Hapi.ServerMethodNext) {
let sum = 0;
array.forEach((item) => {
sum += item;
});
示例3: Server
// https://github.com/hapijs/hapi/blob/master/API.md#-servermethods
import { Server } from "hapi";
const server = new Server({
port: 8000,
});
server.start();
server.method('add', (a, b) => (a + b));
const result = server.methods.add(1, 2); // 3
console.log(result);
示例4: Server
// https://github.com/hapijs/hapi/blob/master/API.md#-servermethodmethods
import {Server, ServerMethodConfigurationObject} from "hapi";
const server = new Server({
port: 8000,
});
server.start();
const add = (a:any, b:any) => {
return a + b;
};
const methodObject:ServerMethodConfigurationObject = {
name: 'sum',
method: add,
options: {
cache: {
expiresIn: 2000,
generateTimeout: 100
}
}
}
server.method(methodObject);