• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript hapi.Server类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Server

(async () => {
  const server = new Server({ debug: false, port: 0 });

  server.register({
    plugin: hapiswagger,
    options: {
      cors: true,
      jsonPath: '/jsonpath',
      info: {
        title: 'a',
        description: 'b',
        termsOfService: 'c',
        contact: {
          name: 'a',
          url: 'localhost',
          email: '[email protected]'
        }
      },
      tagsGroupFilter: (tag: string) => tag !== 'api'
    }
  });

  server.route({
    method: 'GET',
    path: '/',
    handler: () => 'hi',
    options: {
      auth: false,
      plugins: {
        'hapi-swagger': {
          order: 2,
          deprecated: false,
          'x-api-stuff': 'a'
        }
      }
    }
  });

  await server.start();
  await server.stop();
})();
开发者ID:glennjones,项目名称:hapi-swagger,代码行数:41,代码来源:index.test-d.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: 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);
开发者ID:DanielRosenwasser,项目名称:DefinitelyTyped,代码行数:30,代码来源:server-events-once.ts


示例4: Server

// https://github.com/hapijs/hapi/blob/master/API.md#-await-serverauthteststrategy-request
//      https://github.com/hapijs/hapi/blob/master/API.md#-serverauthschemename-scheme
import { Request, ResponseToolkit, Server, ServerAuthScheme, ServerAuthSchemeOptions } from "@hapi/hapi";
import * as Boom from "@hapi/boom";

declare module '@hapi/hapi' {
    interface AuthCredentials {
        name?: string;
    }
}

const server = new Server({
    port: 8000,
});

const scheme: ServerAuthScheme = (server, options) => {
    return {
        authenticate(request, h) {
            const req = request.raw.req;
            const authorization = req.headers.authorization;
            if (!authorization) {
                throw Boom.unauthorized(null, 'Custom');
            }
            return h.authenticated({ credentials: { name: 'john', } });
        }
    };
};

server.auth.scheme('custom', scheme);
server.auth.strategy('default', 'custom');
开发者ID:DanielRosenwasser,项目名称:DefinitelyTyped,代码行数:30,代码来源:server-auth-test.ts


示例5:

server.register(Nes).then(() => {

    server.subscription('/item/{id}');

    return server.start().then(() => {

        server.publish('/item/5', {id: 5, status: 'complete'});
        server.publish('/item/6', {id: 6, status: 'initial'});
    });
})
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:10,代码来源:subscriptions-server.ts


示例6: async

server.register([Basic, Nes]).then(() => {

    // Set up HTTP Basic authentication

    interface User {
        username: string;
        password: string;
        name: string;
        id: string;
    }

    const users: { [index: string]: User } = {
        john: {
            username: 'john',
            password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // 'secret'
            name: 'John Doe',
            id: '2133d32a'
        }
    };

    const validate: Basic.Validate = async (request, username, password, h) => {

        const user = users[username];
        if (!user) {
            return { credentials: null, isValid: false };
        }

        let isValid = await Bcrypt.compare(password, user.password)

        return { isValid, credentials: { id: user.id, name: user.name } };
    };

    server.auth.strategy('simple', 'basic', {validateFunc: validate});
    server.auth.default('simple');

    // Configure route with authentication

    server.route({
        method: 'GET',
        path: '/h',
        options: {
            id: 'hello',
            handler: function (request: Request, h: ResponseToolkit) {

                return 'Hello ' + request.auth.credentials.name;
            }
        }
    });

    return server.start();
});
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:51,代码来源:route-authentication-server.ts


示例7: async

server.register([Basic, Nes]).then(() => {

    // Set up HTTP Basic authentication

    interface User {
        username: string;
        password: string;
        name: string;
        id: string;
    }

    const users: {[index: string]: User} = {
        john: {
            username: 'john',
            password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // 'secret'
            name: 'John Doe',
            id: '2133d32a'
        }
    };

    const validate: Basic.Validate = async (request, username, password, h) => {

        const user = users[username];
        if (!user) {
            return { credentials: null, isValid: false };
        }

        let isValid = await Bcrypt.compare(password, user.password)

        return { isValid, credentials: { id: user.id, name: user.name, username: user.username } };
    };

    server.auth.strategy('simple', 'basic', { validate });
    server.auth.default('simple')

    // Set up subscription

    server.subscription('/items', {
        filter: (path, message, options) => {

            return message.updater !== options.credentials.username;
        }
    });

    server.start().then(() => {

        server.publish('/items', { id: 5, status: 'complete', updater: 'john' });
        server.publish('/items', { id: 6, status: 'initial', updater: 'steve' });
    });
});
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:50,代码来源:subscription-filter-server.ts


示例8:

server.register(Nes).then(() => {

    server.route({
        method: 'GET',
        path: '/h',
        options: {
            id: 'hello',
            handler: (request: Request, h: ResponseToolkit) => {

                return 'world!';
            }
        }
    });

    return server.start();
});
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:16,代码来源:route-invocation-server.ts


示例9:

server.register(Basic).then(() => {

    server.auth.strategy('simple', 'basic', { validate });
	server.auth.default('simple');

    server.route({ method: 'GET', path: '/', options: { auth: 'simple' } });
});
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:7,代码来源:hapi__basic-tests.ts


示例10:

server.register(Nes).then(() => {

    return server.start().then(() => {

        server.broadcast('welcome!');
    });
});
开发者ID:Flarna,项目名称:DefinitelyTyped,代码行数:7,代码来源:broadcast-server.ts



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript command.APIClient类代码示例发布时间:2022-05-28
下一篇:
TypeScript boom.unauthorized函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap