Deno HTTP 服务器
🌙
手机阅读
本文目录结构
HTTP 服务器
以下 Deno 脚本实现了基本的 HTTP 服务器:
代码实现
// Imports `serve` from the remote Deno standard library, using URL.
// 通过URL把远程Deno标准库中的`server.ts` 导入 `serve`。
import { serve } from "https://deno.land/std@v0.21.0/http/server.ts";
// `serve` function returns an asynchronous iterator, yielding a stream of requests
for await (const req of serve({ port: 8000 })) {
req.respond({ body: "Hello World\n" });
}
运行此程序时,Deno 将自动下载并缓存远程标准库文件并编译代码。
命令行实现
类似地,通过提供 URL 作为输入文件名(打开所有权限),我们可以直接运行标准库脚本(例如静态文件服务器)( -A
是打开所有权限):
$ deno run -A https://deno.land/std/http/file_server.ts
Download https://deno.land/std/http/file_server.ts
Compile https://deno.land/std/http/file_server.ts
...
HTTP server listening on http://0.0.0.0:4500/
第一次是这样的,以后再启动就没有下载的步骤了,就是直接使用的了。