Skip to main content
Module

x/masx200_deno_serve_https/serve_http.ts

deno-serve-https 对于deno的低级api的封装,可以启动同时支持http/1.1和http/2的https服务,并且在一个端口上同时支持了http连接升级,websocket,connect方法.
Go to Latest
File
import { Handlers } from "./Handlers.ts";import { hostnameForDisplay } from "./hostnameForDisplay.ts";import { on_Error } from "./on_Error.ts";import { on_NotFound } from "./on_NotFound.ts";import { on_tcp_connection } from "./on_tcp_connection.ts";import { ServeHttpInit } from "./ServeHttpInit.ts";
export async function serve_http( handlers: Handlers = {}, { port = 8000, hostname = "0.0.0.0", onNotFound = on_NotFound, onError = on_Error, ...rest }: ServeHttpInit = {},): Promise<void> { const { signal } = rest; if (signal?.aborted) { return; } const server = Deno.listen({ ...rest, port: port, hostname, });
signal?.addEventListener("abort", () => server.close()); try { if ("onListen" in rest) { rest.onListen?.({ port, hostname }); } else { console.log( `Listening on http://${hostnameForDisplay(hostname)}:${port}/`, ); } for await (const conn of server) { if (signal?.aborted) { return; } on_tcp_connection({ conn, handlers, onError, signal: signal, onNotFound, }).catch(console.error); } } catch (error) { throw error; } finally { try { server.close(); } catch (error) { console.error(error); } }}