本文整理汇总了TypeScript中hapi-latest.Server.ext方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.ext方法的具体用法?TypeScript Server.ext怎么用?TypeScript Server.ext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hapi-latest.Server
的用法示例。
在下文中一共展示了Server.ext方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: start
public async start(config: HttpConfig) {
if (!config.ssl.enabled || config.ssl.redirectHttpFromPort === undefined) {
throw new Error(
'Redirect server cannot be started when [ssl.enabled] is set to `false`' +
' or [ssl.redirectHttpFromPort] is not specified.'
);
}
this.log.info(
`starting HTTP --> HTTPS redirect server [${config.host}:${config.ssl.redirectHttpFromPort}]`
);
// Redirect server is configured in the same way as any other HTTP server
// within the platform with the only exception that it should always be a
// plain HTTP server, so we just ignore `tls` part of options.
this.server = createServer({
...getServerOptions(config, { configureTLS: false }),
port: config.ssl.redirectHttpFromPort,
});
this.server.ext('onRequest', (request: Request, responseToolkit: ResponseToolkit) => {
return responseToolkit
.redirect(
formatUrl({
hostname: config.host,
pathname: request.url.pathname,
port: config.port,
protocol: 'https',
search: request.url.search,
})
)
.takeover();
});
try {
await this.server.start();
} catch (err) {
if (err.code === 'EADDRINUSE') {
throw new Error(
'The redirect server failed to start up because port ' +
`${config.ssl.redirectHttpFromPort} is already in use. Ensure the port specified ` +
'in `server.ssl.redirectHttpFromPort` is available.'
);
} else {
throw err;
}
}
}