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


TypeScript Server.ext方法代码示例

本文整理汇总了TypeScript中@hapi/hapi.Server.ext方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.ext方法的具体用法?TypeScript Server.ext怎么用?TypeScript Server.ext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在@hapi/hapi.Server的用法示例。


在下文中一共展示了Server.ext方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: Server

    });

    /*
     * Request events
     */
    const hash = Crypto.createHash('sha1');

    request.events.on("peek", (chunk) => {
        hash.update(chunk);
    });

    request.events.once("finish", () => {
        console.log(hash.digest('hex'));
    });

    request.events.once("disconnect", () => {
        console.error('request aborted');
    });

    return h.continue;
};

const server = new Server(options);
server.route(serverRoute);
server.ext('onRequest', onRequest, {
    before: 'test',
});

server.start();
console.log('Server started at: ' + server.info.uri);
开发者ID:DanielRosenwasser,项目名称:DefinitelyTyped,代码行数:30,代码来源:event-types.ts

示例2: handler

// https://github.com/hapijs/hapi/blob/master/API.md#-serverextevent-method-options
import { Request, ResponseToolkit, Server, ServerOptions, ServerRoute } from "@hapi/hapi";

const options: ServerOptions = {
    port: 8000,
};

const serverRoute: ServerRoute = {
    path: '/test',
    method: 'GET',
    handler(request, h) {
        return 'ok: ' + request.path;
    }
};

const server = new Server(options);
server.route(serverRoute);

server.ext("onRequest", (request, h) => {
    request.setUrl('/test');
    return h.continue;
});

server.start();
console.log('Server started at: ' + server.info.uri);
开发者ID:DanielRosenwasser,项目名称:DefinitelyTyped,代码行数:25,代码来源:continue.ts

示例3: async

const provision = async () => {
    await server.register(inert);

    await server.register({
        plugin: inert,
        options: { etagsCacheMaxSize: 400 },
    });

    await server.register({
        plugin: inert,
        once: true,
    });

    server.route({
        method: 'GET',
        path: '/{param*}',
        handler: {
            directory: {
                path: '.',
                redirectToSlash: true,
                index: true
            }
        }
    });

    // https://github.com/hapijs/inert#serving-a-single-file
    server.route({
        method: 'GET',
        path: '/{path*}',
        handler: {
            file: 'page.html'
        }
    });

    // https://github.com/hapijs/inert#customized-file-response
    server.route({
        method: 'GET',
        path: '/file',
        handler(request, reply) {
            let path = 'plain.txt';
            if (request.headers['x-magic'] === 'sekret') {
                path = 'awesome.png';
            }

            return reply.file(path).vary('x-magic');
        }
    });

    const handler: Lifecycle.Method = (request, h) => {
        const response = request.response;
        if (response instanceof Error && response.output.statusCode === 404) {
            return h.file('404.html').code(404);
        }

        return h.continue;
    };

    server.ext('onPostHandler', handler);

    const file: inert.FileHandlerRouteObject = {
        path: '',
        confine: true,
    };

    const directory: inert.DirectoryHandlerRouteObject = {
        path: '',
        listing: true
    };

    server.route({
        path: '',
        method: 'GET',
        handler: {
            file,
            directory: {
                path() {
                    if (Math.random() > 0.5) {
                        return '';
                    } else if (Math.random() > 0) {
                        return [''];
                    }
                    return new Error('');
                }
            },
        },
        options: { files: { relativeTo: __dirname } }
    });
};
开发者ID:DanielRosenwasser,项目名称:DefinitelyTyped,代码行数:88,代码来源:hapi__inert-tests.ts


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