本文整理匯總了TypeScript中hapi.Server類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server類的具體用法?TypeScript Server怎麽用?TypeScript Server使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Server類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Server
// https://github.com/hapijs/hapi/blob/master/API.md#-await-serverstart
import { Server } from "hapi";
const server = new Server({
port: 8000,
});
server.start();
server.events.on('start', () => {
console.log('Server started at: ' + server.info.uri);
});
示例2: Server
// https://github.com/hapijs/hapi/blob/master/API.md#catch-all-route
import { Request, ResponseToolkit, Server, ServerOptions } from "hapi";
const options: ServerOptions = {
port: 8000,
};
const server = new Server(options);
const handler = (request: Request, h: ResponseToolkit) => {
return h.response('The page was not found').code(404);
};
server.route({ method: '*', path: '/{p*}', handler });
server.start();
console.log('Server started at: ' + server.info.uri);
示例3: reply
// From https://hapijs.com/api/16.1.1#serverextevents
import * as Hapi from 'hapi';
const server = new Hapi.Server();
server.connection({ port: 80 });
server.ext({
type: 'onRequest',
method: function (request, reply) {
// Change all requests to '/test'
request.setUrl('/test');
return reply.continue();
}
});
var handler: Hapi.RouteHandler = function (request, reply) {
return reply({ status: 'ok' });
};
server.route({ method: 'GET', path: '/test', handler: handler });
server.start((err) => { });
// All requests will get routed to '/test'
// Example 2
server.ext('onRequest', function (request, reply) {
示例4: getSpacesUsage
/**
*
* @param callCluster
* @param server
* @param {boolean} spacesAvailable
* @return {UsageStats}
*/
async function getSpacesUsage(callCluster: any, server: Server, spacesAvailable: boolean) {
if (!spacesAvailable) {
return {} as UsageStats;
}
const index = server.config().get('kibana.index');
const knownFeatureIds = server.plugins.xpack_main.getFeatures().map(feature => feature.id);
const resp = await callCluster('search', {
index,
body: {
track_total_hits: true,
query: {
term: {
type: {
value: 'space',
},
},
},
aggs: {
disabledFeatures: {
terms: {
field: 'space.disabledFeatures',
include: knownFeatureIds,
size: knownFeatureIds.length,
},
},
},
size: 0,
},
});
const { hits, aggregations } = resp;
const count = get(hits, 'total.value', 0);
const disabledFeatureBuckets = get(aggregations, 'disabledFeatures.buckets', []);
const initialCounts = knownFeatureIds.reduce(
(acc, featureId) => ({ ...acc, [featureId]: 0 }),
{}
);
const disabledFeatures: Record<string, number> = disabledFeatureBuckets.reduce(
(acc, { key, doc_count }) => {
return {
...acc,
[key]: doc_count,
};
},
initialCounts
);
const usesFeatureControls = Object.values(disabledFeatures).some(
disabledSpaceCount => disabledSpaceCount > 0
);
return {
count,
usesFeatureControls,
disabledFeatures,
} as UsageStats;
}
示例5:
// 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) => {
示例6: Server
// from https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncecriteria-listener
import {Request, ResponseToolkit, Server, ServerRoute} from "hapi";
const serverRoute: ServerRoute = {
path: '/',
method: 'GET',
handler: (request: Request, h: ResponseToolkit) => {
return 'oks: ' + request.path;
}
};
const server = new Server({
port: 8000,
});
server.route(serverRoute);
server.event('test1');
server.event('test2');
server.events.once('test1', (update: any) => {console.log(update);});
server.events.once('test2', (...args: any[]) => {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);
示例7: require
// from https://github.com/hapijs/hapi-auth-basic#hapi-auth-basic
import Bcrypt = require('bcrypt');
import Basic = require('hapi-auth-basic');
import * as Hapi from 'hapi';
const server = new Hapi.Server();
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 { isValid: false, credentials: null };
}
示例8:
register: (server:Server, options:ServerRegisterOptions) => {
server.expose('key', 'value');
server.plugins.example.other = 'other';
console.log(server.plugins.example.key); // 'value'
console.log(server.plugins.example.other); // 'other'
}
示例9: reply
import * as hapi from 'hapi';
import { LangItem, data } from './data';
const server = new hapi.Server();
server.connection({
port: 8999
});
server.start((err) => {
if(err) {
console.log(err);
throw err;
}
console.log('server running at:', server.info.uri);
})
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => {
return reply('flashcards api');
}
});
server.route({
method: 'GET',
path: '/lang-item/{id}',