summaryrefslogtreecommitdiff
path: root/std/http/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/http/server.ts')
-rw-r--r--std/http/server.ts49
1 files changed, 34 insertions, 15 deletions
diff --git a/std/http/server.ts b/std/http/server.ts
index 9e2184351..db4150ef5 100644
--- a/std/http/server.ts
+++ b/std/http/server.ts
@@ -383,10 +383,28 @@ export class Server implements AsyncIterable<ServerRequest> {
}
}
-export function serve(addr: string): Server {
- // TODO(ry) Update serve to also take { hostname, port }.
- const [hostname, port] = addr.split(":");
- const listener = listen({ hostname, port: Number(port) });
+interface ServerConfig {
+ port: number;
+ hostname?: string;
+}
+
+/**
+ * Start a HTTP server
+ *
+ * import { serve } from "https://deno.land/std/http/server.ts";
+ * const body = new TextEncoder().encode("Hello World\n");
+ * const s = serve({ port: 8000 });
+ * for await (const req of s) {
+ * req.respond({ body });
+ * }
+ */
+export function serve(addr: string | ServerConfig): Server {
+ if (typeof addr === "string") {
+ const [hostname, port] = addr.split(":");
+ addr = { hostname, port: Number(port) };
+ }
+
+ const listener = listen(addr);
return new Server(listener);
}
@@ -406,19 +424,20 @@ export type HTTPSOptions = Omit<Deno.ListenTLSOptions, "transport">;
/**
* Create an HTTPS server with given options
+ *
+ * const body = new TextEncoder().encode("Hello HTTPS");
+ * const options = {
+ * hostname: "localhost",
+ * port: 443,
+ * certFile: "./path/to/localhost.crt",
+ * keyFile: "./path/to/localhost.key",
+ * };
+ * for await (const req of serveTLS(options)) {
+ * req.respond({ body });
+ * }
+ *
* @param options Server configuration
* @return Async iterable server instance for incoming requests
- *
- * const body = new TextEncoder().encode("Hello HTTPS");
- * const options = {
- * hostname: "localhost",
- * port: 443,
- * certFile: "./path/to/localhost.crt",
- * keyFile: "./path/to/localhost.key",
- * };
- * for await (const req of serveTLS(options)) {
- * req.respond({ body });
- * }
*/
export function serveTLS(options: HTTPSOptions): Server {
const tlsOptions: Deno.ListenTLSOptions = {