我正在寻找在Deno中创建https服务器的示例。我已经看到了Deno http服务器的示例,但没有看到https。
我尝试在Google中搜索,但未找到结果
答案 0 :(得分:1)
serveTLS
与Deno 0.23.0一同登陆:
样品用量:
import { serveTLS } from "https://deno.land/std/http/server.ts";
const body = new TextEncoder().encode("Hello HTTPS");
const options = {
hostname: "localhost",
port: 443,
certFile: "./path/to/localhost.crt",
keyFile: "./path/to/localhost.key",
};
// Top-level await supported
for await (const req of serveTLS(options)) {
req.respond({ body });
}
答案 1 :(得分:1)
Dano现在支持TLS绑定。以下是创建https服务器的方法:
import { serveTLS } from "https://deno.land/std/http/server.ts";
const body = new TextEncoder().encode("Hello HTTPS");
const options = {
hostname: "localhost",
port: 443,
certFile: "./path/to/localhost.crt",
keyFile: "./path/to/localhost.key",
};
for await (const req of serveTLS(options)) {
req.respond({ body });
}
serveTLS
参数
options: any
返回:Server
使用listenAndServeTLS
listenAndServeTLS(options, (req) => {
req.respond({ body });
});
listenAndServeTLS
参数
options: any
handler: (req: ServerRequest) => void
返回:任意
答案 2 :(得分:0)
您是否检查过DENO ABC,这是一个用于创建Web应用程序的更好的Deno框架。
import { abc } from "https://deno.sh/abc/mod.ts";
const app = abc();
app
.get("/hello", c => {
return "Hello, Abc!";
})
.start("0.0.0.0:8080");
答案 3 :(得分:0)
答案 4 :(得分:0)
首先,可以使用 DENO std 库创建 HTTPS 服务器。但就我而言,我为我的应用程序使用了 OAK 库。可以在 here 中找到有关 Oak 图书馆的更多信息。 步骤 1:准备好证书文件和密钥文件(假设它们是为您喜欢的任何域名生成的。它可以只是 localhost)。如果您不知道这是什么意思,请阅读this article.。 第 2 步:是时候配置应用的监听选项了。您可以复制以下代码行并根据需要更改 certFile 和 keyFile 选项的路径。下面给出更多解释。
await app.listen({ port: port, secure: true, certFile: "<path-to-file>/<file-name>.pem", keyFile: "<path-to-file>/<file-name>-key.pem" });
如果你想知道上面这行发生了什么: