本文整理匯總了TypeScript中@hapi/hapi.Server.event方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.event方法的具體用法?TypeScript Server.event怎麽用?TypeScript Server.event使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@hapi/hapi.Server
的用法示例。
在下文中一共展示了Server.event方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: handler
import { Request, ResponseToolkit, Server, ServerRoute } from "@hapi/hapi";
const serverRoute: ServerRoute = {
path: '/',
method: 'GET',
handler(request, h) {
return 'oks: ' + request.path;
}
};
declare module '@hapi/hapi' {
interface ServerEvents {
once(event: 'test1', listener: (update: string) => void): this;
once(event: 'test2', listener: (...updates: string[]) => void): this;
}
}
const server = new Server({
port: 8000,
});
server.route(serverRoute);
server.event('test1');
server.event('test2');
server.events.once('test1', update => { console.log(update); });
server.events.once('test2', (...args) => { console.log(args); });
server.events.emit('test1', 'hello-1');
server.events.emit('test2', 'hello-2'); // Ignored
server.start();
console.log('Server started at: ' + server.info.uri);
示例2: require
// https://github.com/hapijs/hapi/blob/master/API.md#-servereventevents
import { Server, ServerEvents } from "@hapi/hapi";
import Podium = require('@hapi/podium');
import '@hapi/hapi/definitions/server/server';
declare module '@hapi/hapi' {
interface ServerEvents {
on(event: 'test', listener: (update: string) => void): this;
}
}
const server = new Server({
port: 8000,
});
server.events.on('route', route => {
console.log(route.path, route.vhost, route.realm, route.method, route.settings, route.fingerprint);
});
server.event('test');
server.events.on('test', (update: any) => console.log(update));
server.events.emit('test', 'hello');
server.start();