mirror of
https://github.com/denoland/deno.git
synced 2024-12-29 02:29:06 -05:00
Allow http server to take { hostname, port } argument (#3233)
This commit is contained in:
parent
5c1deac0cf
commit
ccc9f1ae5e
1 changed files with 34 additions and 15 deletions
|
@ -383,10 +383,28 @@ export class Server implements AsyncIterable<ServerRequest> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serve(addr: string): Server {
|
interface ServerConfig {
|
||||||
// TODO(ry) Update serve to also take { hostname, port }.
|
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(":");
|
const [hostname, port] = addr.split(":");
|
||||||
const listener = listen({ hostname, port: Number(port) });
|
addr = { hostname, port: Number(port) };
|
||||||
|
}
|
||||||
|
|
||||||
|
const listener = listen(addr);
|
||||||
return new Server(listener);
|
return new Server(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,8 +424,6 @@ export type HTTPSOptions = Omit<Deno.ListenTLSOptions, "transport">;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an HTTPS server with given options
|
* Create an HTTPS server with given options
|
||||||
* @param options Server configuration
|
|
||||||
* @return Async iterable server instance for incoming requests
|
|
||||||
*
|
*
|
||||||
* const body = new TextEncoder().encode("Hello HTTPS");
|
* const body = new TextEncoder().encode("Hello HTTPS");
|
||||||
* const options = {
|
* const options = {
|
||||||
|
@ -419,6 +435,9 @@ export type HTTPSOptions = Omit<Deno.ListenTLSOptions, "transport">;
|
||||||
* for await (const req of serveTLS(options)) {
|
* for await (const req of serveTLS(options)) {
|
||||||
* req.respond({ body });
|
* req.respond({ body });
|
||||||
* }
|
* }
|
||||||
|
*
|
||||||
|
* @param options Server configuration
|
||||||
|
* @return Async iterable server instance for incoming requests
|
||||||
*/
|
*/
|
||||||
export function serveTLS(options: HTTPSOptions): Server {
|
export function serveTLS(options: HTTPSOptions): Server {
|
||||||
const tlsOptions: Deno.ListenTLSOptions = {
|
const tlsOptions: Deno.ListenTLSOptions = {
|
||||||
|
|
Loading…
Reference in a new issue