Skip to content

Instantly share code, notes, and snippets.

@irustm
Created April 1, 2021 06:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irustm/cf534f3cf1b89010c06761f48f6bc657 to your computer and use it in GitHub Desktop.
Save irustm/cf534f3cf1b89010c06761f48f6bc657 to your computer and use it in GitHub Desktop.
Fast http on Deno
async function serve() {
const addr = Deno.args[0] || "127.0.0.1:4500";
const [hostname, port] = addr.split(":");
const listener = Deno.listen({ hostname, port: Number(port) });
const response = new TextEncoder().encode(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
);
const decoder = new TextDecoder();
async function handle(conn: Deno.Conn): Promise<void> {
if(!conn) return ;
let buffer = new Uint8Array(1024);
try {
while (true) {
const r = await conn.read(buffer);
if (r === null) {
break;
}
// const text = decoder.decode(buffer);
// buffer = new Uint8Array(1024);
// console.log(text)
await conn.write(response);
}
} finally {
conn.close();
}
}
for await (const conn of listener) {
handle(conn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment