Skip to content

Instantly share code, notes, and snippets.

@aasmpro
Created March 24, 2022 20:48
Show Gist options
  • Save aasmpro/0dc82c7b7ae87ddad354c2bedf079ceb to your computer and use it in GitHub Desktop.
Save aasmpro/0dc82c7b7ae87ddad354c2bedf079ceb to your computer and use it in GitHub Desktop.
Simple server with deno
import { listenAndServe } from "https://deno.land/[email protected]/http/server.ts";
class Server {
options: any = {port: 8000}
routes: any = {}
handle = (path: any, func: any) => {
this.routes[path] = func
}
serve = (req: any) => {
console.log(req.url);
try {
this.routes[req.url](req);
} catch {
req.respond({body: "404, Page not found."});
}
}
start = () => {
listenAndServe(this.options, this.serve)
}
}
const server = new Server();
const index = (req: any) => {
return req.respond({body: "Hello World!\n"});
}
const me = (req: any) => {
return req.respond({body: "Yep! you found me!\n"});
}
server.handle("/", index)
server.handle("/me/", me)
server.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment