使用自签名证书在 Deno 上设置 HTTPS?

Anu*_*hra 5 deno

我正在设置 Deno 服务器来处理 HTTPS 请求,我使用自签名证书来完成这项工作。\n为此使用了以下代码:

\n\n
import { serveTLS } from "https://deno.land/std/http/server.ts";\n\nconst body = new TextEncoder().encode("Hello HTTPS");\nconst options = {\n  hostname: "localhost",\n  port: 443,\n  certFile: "./path/to/localhost.crt",\n  keyFile: "./path/to/localhost.key",\n};\n// Top-level await supported\nfor await (const req of serveTLS(options)) {\n  req.respond({ body });\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我运行此代码为:deno --allow-net --allow-read app.ts\n我收到以下错误:

\n\n
ERROR RS - rustls::session:571 - TLS alert received: Message {\n    typ: Alert,\n    version: TLSv1_3,\n    payload: Alert(\n        AlertMessagePayload {\n            level: Fatal,\n            description: BadCertificate,\n        },\n    ),\n}\nerror: Uncaught InvalidData: received fatal alert: BadCertificate\n\xe2\x96\xba $deno$/errors.ts:57:13\n    at InvalidData ($deno$/errors.ts:135:5)\n    at constructError ($deno$/errors.ts:57:13)\n    at unwrapResponse ($deno$/dispatch_json.ts:41:12)\n    at sendAsync ($deno$/dispatch_json.ts:96:10)\n
Run Code Online (Sandbox Code Playgroud)\n\n

是否可以在 Deno 中使用自签名证书?\n出了什么问题以及如何修复它?

\n

Anu*_*hra 2

这是我的证书文件的问题,奇怪的是,相同的证书在 Nodejs 上工作。我创建了本地证书和密钥,而不是使用此mkcert,然后它就起作用了!