mirror of
https://github.com/denoland/deno.git
synced 2024-11-30 16:40:57 -05:00
Init
This commit is contained in:
commit
60735e1804
2 changed files with 46 additions and 0 deletions
42
http.ts
Normal file
42
http.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import * as deno from "deno";
|
||||
|
||||
class Server {
|
||||
_closing = false;
|
||||
|
||||
constructor(readonly listener: deno.Listener) {}
|
||||
|
||||
async serveConn(conn: deno.Conn) {
|
||||
const buffer = new Uint8Array(1024);
|
||||
try {
|
||||
while (true) {
|
||||
const r = await conn.read(buffer);
|
||||
if (r.eof) {
|
||||
break;
|
||||
}
|
||||
const response = new TextEncoder().encode(
|
||||
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
|
||||
);
|
||||
await conn.write(response);
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
async serve() {
|
||||
while (!this._closing) {
|
||||
const conn = await this.listener.accept();
|
||||
this.serveConn(conn);
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this._closing = true;
|
||||
}
|
||||
}
|
||||
|
||||
export function listen(addr: string): Server {
|
||||
const listener = deno.listen("tcp", addr);
|
||||
const s = new Server(listener);
|
||||
return s;
|
||||
}
|
4
http_test.ts
Normal file
4
http_test.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { listen } from "./server.ts";
|
||||
|
||||
const s = listen("0.0.0.0:4500");
|
||||
s.serve();
|
Loading…
Reference in a new issue