【问题标题】:Creating a Deno https server创建 Deno https 服务器
【发布时间】:2019-09-18 15:15:24
【问题描述】:

我正在寻找在 Deno 中创建 https 服务器的示例。我见过 Deno http 服务器的例子,但不是 https。

我已经尝试在谷歌搜索但没有找到结果

【问题讨论】:

  • Deno 目前还没有 SSL 绑定。正在进行添加此类绑定的工作,因此您可以预期它会在未来 2 个月内落地。
  • 如上所述,目前在 Deno 中没有办法这样做。一个不错的选择是设置一个像 nginx 这样的反向代理,它将 https 请求转发到您的 http 服务器。
  • 小更新:我们刚刚实现了Deno.listenTLS()。我们将很快在 Deno 标准模块中为 https 引入 serveTLS 或类似内容。

标签: deno


【解决方案1】:

使用 Deno 1.9+,您可以使用本机服务器。它提供了使用 HTTPS 的能力。

它比std/http 的旧实现快good bit faster。但是,从版本 0.107.0 开始,本机服务器也用于 std/http

例子:

const server = Deno.listenTls({
  port: 443,
  certFile: "./my-ca-certificate.pem",
  keyFile: "./my-key.pem"
});

for await (const conn of server) {
  handle(conn);
}

async function handle(conn: Deno.Conn) {
  const httpConn = Deno.serveHttp(conn);
  
  for await (const requestEvent of httpConn) {
    try {
      const response = new Response("Hello World!");
      await requestEvent.respondWith(response);
    } 
    catch (error) {
      console.error(error);
    }
  }
}

【讨论】:

    【解决方案2】:

    首先,可以使用 DENO 标准库创建一个 HTTPS 服务器。但就我而言,我为我的应用程序使用了 OAK 库。更多关于橡树图书馆的信息可以找到here。 步骤 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 的应用程序的侦听方法接受要配置的选项,这些选项的类型为ListenOptions,可以是ListenOptionsBaseListenOptionsTls,它们分别继承自Deno.ListenOptionsDeno.ListenTlsOptions。如果您检查 Deno.ListenTlsOptions,则有两个选项,即 certFile 和 keyFile,它们分别接受证书的路径和证书的密钥,它们都是 .pem 文件。

    【讨论】:

      【解决方案3】:

      使用 deno Oak 框架怎么样?

      https://github.com/oakserver/oak

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

      【讨论】:

        【解决方案4】:

        您检查过 DENO ABC 吗?它是创建 Web 应用程序的更好框架。在https://deno.land/x/abc/README.md 阅读更多内容。

        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");
        

        【讨论】:

          【解决方案5】:

          现在 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) =&gt; void

          返回:任意

          For more details see official docs:

          【讨论】:

          • 你知道如何让它与自签名证书一起使用吗,它似乎无法使用它!
          • 是的,我收到此错误:Uncaught InvalidData: received fatal alert: BadCertificate,相同的证书密钥适用于节点快速设置!
          【解决方案6】:

          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 });
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-03-01
            • 1970-01-01
            • 2013-08-20
            • 2017-09-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多