当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Server.on方法代码示例

本文整理汇总了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);
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:11,代码来源:emit.ts

示例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');
});
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:25,代码来源:tail.ts

示例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');
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:14,代码来源:log.ts

示例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);
    }
});
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:29,代码来源:state.ts

示例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();
};
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:18,代码来源:log.ts

示例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) => {
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:29,代码来源:on.ts


注:本文中的hapi.Server.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。