Created
April 1, 2021 06:26
-
-
Save irustm/cf534f3cf1b89010c06761f48f6bc657 to your computer and use it in GitHub Desktop.
Fast http on Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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