本文整理汇总了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);
示例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);
示例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 } }
});
};