本文整理匯總了TypeScript中@hapi/hapi.Server.start方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.start方法的具體用法?TypeScript Server.start怎麽用?TypeScript Server.start使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@hapi/hapi.Server
的用法示例。
在下文中一共展示了Server.start方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
server.register(Nes).then(() => {
return server.start().then(() => {
server.broadcast('welcome!');
});
});
示例2:
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'});
});
})
示例3: 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();
});
示例4: 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' });
});
});
示例5:
server.register(Nes).then(() => {
server.route({
method: 'GET',
path: '/h',
options: {
id: 'hello',
handler: (request: Request, h: ResponseToolkit) => {
return 'world!';
}
}
});
return server.start();
});
示例6: 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();
})();
示例7: Server
// https://github.com/hapijs/hapi/blob/master/API.md#-requestlogtags-data
import { Lifecycle, Request, ResponseToolkit, Server, ServerOptions, ServerRoute } from "@hapi/hapi";
const options: ServerOptions = {
port: 8000,
};
const handlerFn: Lifecycle.Method = (request, h) => {
request.log(['test', 'error'], 'Test event');
return 'path: ' + request.path;
};
const serverRoute: ServerRoute = {
path: '/',
method: 'GET',
handler: handlerFn
};
const server = new Server(options);
server.route(serverRoute);
server.start();
console.log('Server started at: ' + server.info.uri);
server.events.on('request', (request: Request, event: any, tags: any) => {
console.log(tags);
if (tags.error) {
console.log(event);
}
});