创建一个Deno https服务器

时间:2019-04-30 01:31:09

标签: deno

我正在寻找在Deno中创建https服务器的示例。我已经看到了Deno http服务器的示例,但没有看到https。

我尝试在Google中搜索,但未找到结果

5 个答案:

答案 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

返回:任意

For more details see official docs:

答案 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)

如何使用deno Oak框架?

https://github.com/oakserver/oak

我认为该项目是Deno中最稳定的Web框架。 而且您还从中获得了很多想要了解的信息。

答案 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" });

如果你想知道上面这行发生了什么:

  1. Oak 的应用程序的 listen 方法接受要配置的选项,这些选项属于 ListenOptions 类型,可以是 ListenOptionsBaseListenOptionsTls,它们继承自 Deno.ListenOptions 和 {{ 3}} 分别。如果您检查 Deno.ListenTlsOptions,则有两个选项是 certFile 和 keyFile,它们分别接受证书的路径和证书的密钥,它们都是 .pem 文件。