本文整理汇总了TypeScript中hapi.Server.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.on方法的具体用法?TypeScript Server.on怎么用?TypeScript Server.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hapi.Server
的用法示例。
在下文中一共展示了Server.on方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
// From https://hapijs.com/api/16.1.1#serveremitcriteria-data-callback
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
server.event('test');
server.on('test', (update: Update) => console.log(update));
type Update = string;
var toSend: Update = 'hello';
server.emit('test', toSend);
示例2: function
// From https://hapijs.com/api/16.1.1#requestsetmethodmethod
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
const get: Hapi.RouteHandler = function (request, reply) {
const dbTail = request.tail('write to database');
var db: any;
db.save('key', 'value', () => {
dbTail();
});
return reply('Success!');
};
server.route({ method: 'GET', path: '/', handler: get });
server.on('tail', (request) => {
console.log('Request completed including db activity');
});
示例3:
// From https://hapijs.com/api/16.1.1#serverlogtags-data-timestamp
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
server.on('log', (event, tags) => {
if (tags.error) {
console.log(event);
}
});
server.log(['test', 'error'], 'Test event');
示例4: function
server.state('session', {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: true,
path: '/',
encoding: 'base64json'
});
// Set state in route handler
const handler: Hapi.RouteHandler = function (request, reply) {
let session = request.state.session;
if (!session) {
session = { user: 'joe' };
}
session.last = Date.now();
return reply('Success').state('session', session);
};
// Example 2
server.on('request-internal', (request, event, tags) => {
if (tags.error && tags.state) {
console.error(event);
}
});
示例5: function
// From https://hapijs.com/api/16.1.1#requestlogtags-data-timestamp
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80, routes: { log: true, security: false } });
server.on('request', (request, event, tags) => {
if (tags.error) {
console.log(event);
}
});
const handler: Hapi.RouteHandler = function (request, reply) {
request.log(['test', 'error'], 'Test event');
return reply();
};
示例6:
// From https://hapijs.com/api/16.1.1#serveroncriteria-listener
// From https://hapijs.com/api/16.1.1#server-events
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
server.event('test');
server.on('test', (update: Update) => console.log(update));
type Update = string;
var toSend: Update = 'hello';
server.emit('test', toSend);
server.on('log', (event, tags) => {
if (tags.error) {
console.log('Server error: ' + (event.data || 'unspecified'));
}
});
server.on('request', (request, event, tags) => {
if (tags.received) {
console.log('New request: ' + request.id);
}
});
server.on('request-error', (request, err) => {