本文整理汇总了TypeScript中@hapi/hapi.Server.register方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.register方法的具体用法?TypeScript Server.register怎么用?TypeScript Server.register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@hapi/hapi.Server
的用法示例。
在下文中一共展示了Server.register方法的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: 'who@where.com'
}
},
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();
})();
示例2: async
import * as hapi from '@hapi/hapi';
import * as crumb from '@hapi/crumb';
const server = new hapi.Server({ port: 8000 });
server.register({
plugin: crumb,
options: {
key: 'csrf-token',
size: 50,
autoGenerate: true,
addToViewContext: true,
cookieOptions: {
path: '/',
},
headerName: 'X-CSRF-Token',
restful: true,
skip: () => false,
enforce: true,
logUnauthorized: false,
}
});
server.route({
method: 'get',
path: '/',
handler: async (_, h) => {
return h.continue;
},
});
示例3: Server
// https://github.com/hapijs/hapi/blob/master/API.md#-serverbindcontext
import { Lifecycle, Plugin, Request, ResponseToolkit, Server, ServerRegisterOptions } from "@hapi/hapi";
const server = new Server({
port: 8000,
});
const handler: Lifecycle.Method = (request, h) => {
return h.context.message; // Or h.context.message
};
const plugin: Plugin<any> = {
name: 'example',
register: async (server, options) => {
const bind = {
message: 'hello'
};
server.bind(bind);
server.route({ method: 'GET', path: '/', handler });
}
};
server.start();
server.register(plugin);
server.events.on('start', () => {
console.log('Server started at: ' + server.info.uri);
});
示例4: beforeEach
beforeEach(async () => {
server = new Hapi.Server({ debug: { request: ["*"] } });
await server.register({
plugin,
options: {
pagination: {
routes: {
include: [],
},
},
},
});
const { Serializer } = server.plugins["@hapist/json-api"];
// Register 'article' type
Serializer.register("article", {
id: "id", // The attributes to use as the reference. Default = 'id'.
blacklist: ["updated"], // An array of blacklisted attributes. Default = []
links: {
// An object or a function that describes links.
self(data) {
// Can be a function or a string value ex: { self: '/articles/1'}
return "/articles/" + data.id;
},
},
relationships: {
// An object defining some relationships.
author: {
type: "people", // The type of the resource
links(data) {
// An object or a function that describes Relationships links
return {
self: "/articles/" + data.id + "/relationships/author",
related: "/articles/" + data.id + "/author",
};
},
},
tags: {
type: "tag",
},
photos: {
type: "photo",
},
comments: {
type: "comment",
schema: "only-body", // A custom schema
},
},
topLevelMeta(data, extraData) {
// An object or a function that describes top level meta.
return {
count: extraData.count,
total: data.length,
};
},
topLevelLinks: {
// An object or a function that describes top level links.
self: "/articles", // Can be a function (with extra data argument) or a string value
},
});
// Register 'people' type
Serializer.register("people", {
id: "id",
links: {
self(data) {
return "/peoples/" + data.id;
},
},
});
// Register 'tag' type
Serializer.register("tag", {
id: "id",
});
// Register 'photo' type
Serializer.register("photo", {
id: "id",
});
// Register 'comment' type with a custom schema
Serializer.register("comment", "only-body", {
id: "_id",
});
});
示例5: require
import { AuthCredentials, Request, ResponseToolkit, Server } from '@hapi/hapi';
import Basic = require('@hapi/basic');
import Bcrypt = require('bcrypt');
import Nes = require('@hapi/nes');
const server = new Server();
declare module '@hapi/hapi' {
interface AuthCredentials {
id: string;
name: string;
}
}
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',
示例6: async
register: async (server: Server, options: Plugin3) => {},
dependencies: ['plugin2'],
};
const plugin4: Plugin<Plugin4> = {
name: 'plugin4',
register: (server: Server, options: Plugin4) => {},
dependencies: 'plugin3',
};
const server = new Server({
port: 8000,
});
server.start();
server.register(plugin1);
server.register({
plugin: plugin1,
options: {one: 1}
});
server.register([
{
plugin: plugin2,
options: {two: 2}
},
{
plugin: plugin3,
options: {three: 3}
},
示例7: util
example2: {
util(): void;
};
}
}
const plugin1: Plugin<any> = {
name: 'example1',
async register(server: Server, options: ServerRegisterOptions) {
server.expose('util', () => console.log('something'));
}
};
const plugin2: Plugin<any> = {
name: 'example2',
async register(server: Server, options: ServerRegisterOptions) {
server.expose('util', () => console.log('something'));
}
};
const server = new Server({
port: 8000,
});
server.start();
server.register(plugin1);
server.register(plugin2);
server.plugins.example1.util();
server.plugins.example2.util();
示例8: 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 } }
});
};
示例9: async
const provision = async () => {
await server.register({
plugin: Vision,
options: {
engines: { hbs: Handlebars },
path: __dirname + '/templates',
}
});
server.views({
engines: { hbs: Handlebars },
path: __dirname + '/templates',
});
const context = {
title: 'Views Example',
message: 'Hello, World',
};
console.log(await server.render('hello', context));
server.route({
method: 'GET',
path: '/view',
handler: async (request: Request, h: ResponseToolkit) => {
return request.render('test', { message: 'hello' });
},
});
server.route({
method: 'GET',
path: '/',
handler: {
view: {
template: 'hello',
context: {
title: 'Views Example',
message: 'Hello, World',
},
},
},
});
const handler = (request: Request, h: ResponseToolkit) => {
const context = {
title: 'Views Example',
message: 'Hello, World',
};
return h.view('hello', context);
};
server.route({ method: 'GET', path: '/', handler });
server.route({
method: 'GET',
path: '/temp1',
handler: {
view: {
template: 'temp1',
options: {
compileOptions: {
noEscape: true,
},
},
},
},
});
};
示例10: run
async function run() {
const server = new Server({ port: 8000 });
await server.register(bell);
bell.simulate(async () => ({}));
bell.simulate(() => ({}));
server.auth.strategy('arcgisonline', 'bell', {
provider: 'twitter',
password: 'some cookie password',
location: 'http://example.com/oauth',
clientId: '',
clientSecret: '',
scope(request) {
const scopes = ['public_profile', 'email'];
if (request.query.wantsSharePermission) {
scopes.push('publish_actions');
}
return scopes;
}
});
const providers: bell.BellOptions[] = [{
provider: 'discord',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: '',
location: () => '',
},
{
provider: 'facebook',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: '',
location: server.info.uri
},
{
provider: 'google',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: '',
location: server.info.uri
},
{
provider: 'linkedin',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: '',
providerParams: {
redirect_uri: server.info.uri + '/bell/door'
}
},
{
provider: 'meetup',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: '',
scope: ['basic', 'ageless', 'group_edit', 'reporting'],
},
{
provider: 'nest',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: ''
},
{
provider: 'office365',
clientId: '',
clientSecret: '',
password: 'cookie_encryption_password_secure',
providerParams: {
response_type: 'code'
},
scope: ['openid', 'offline_access', 'profile']
},
{
provider: 'okta',
config: { uri: 'https://your-organization.okta.com' },
password: 'cookie_encryption_password_secure',
isSecure: false,
location: 'http://127.0.0.1:8000',
clientId: '',
clientSecret: ''
},
{
provider: 'slack',
password: 'cookie_encryption_password_secure',
isSecure: false,
clientId: '',
clientSecret: ''
},
{
provider: 'twitch',
password: 'cookie_encryption_password_secure',
//.........这里部分代码省略.........