Created
March 24, 2022 20:48
-
-
Save aasmpro/0dc82c7b7ae87ddad354c2bedf079ceb to your computer and use it in GitHub Desktop.
Simple server with 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
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