
恰巧今日有朋友聊到 deno,学习了下 deno,顺手写了个小玩具。(内容一会再补充……)
前端工程师无缝入手。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import { serve } from "https://deno.land/[email protected]/http/server.ts"; const server = serve({ port: 8899 }); const CONTENT_TYPE_MAP: { [index: string]: any } = { html: "text/html; charset=UTF-8", htm: "text/html; charset=UTF-8", js: "application/javascript; charset=UTF-8", css: "text/css; charset=UTF-8", txt: "text/plain; charset=UTF-8", mainfest: "text/plain; charset=UTF-8", }; (async function () { for await (const req of server) { const pathname = req.url.split("?")[0]; const filename = "/Users/vernbrandl/Downloads/" + pathname.substring(1); const suffix = filename.substring(filename.lastIndexOf(".") + 1); try { const content = await Deno.readFile(filename); const headers = new Headers({ "Content-Type": CONTENT_TYPE_MAP[suffix] || "application/octet-stream", }); req.respond({ body: content, status: 200, headers, }); } catch (error) { req.respond({ body: "Not Found\n", status: 404 }); } } })();
|
项目地址 => https://github.com/tkvern/deno-http-server