deno.com
本页内容

TCP 套接字和 TLS

Deno Deploy 支持出站 TCP 和 TLS 连接。这些 API 使您可以在 Deploy 上使用 PostgreSQL、SQLite、MongoDB 等数据库。

正在寻找有关提供 TCP 服务的信息?请查看 Deno.serve 的文档,包括它对 TCP 选项的支持。

Deno.connect 跳转到标题

建立出站 TCP 连接。

该函数定义与 Deno 相同,但有以下限制:transport 选项只能是 tcp,且 hostname 不能是 localhost 或空。

function Deno.connect(options: ConnectOptions): Promise<Conn>

示例 跳转到标题

async function handler(_req) {
  // Make a TCP connection to example.com
  const connection = await Deno.connect({
    port: 80,
    hostname: "example.com",
  });

  // Send raw HTTP GET request.
  const request = new TextEncoder().encode(
    "GET / HTTP/1.1\nHost: example.com\r\n\r\n",
  );
  const _bytesWritten = await connection.write(request);

  // Read 15 bytes from the connection.
  const buffer = new Uint8Array(15);
  await connection.read(buffer);
  connection.close();

  // Return the bytes as plain text.
  return new Response(buffer, {
    headers: {
      "content-type": "text/plain;charset=utf-8",
    },
  });
}

Deno.serve(handler);

Deno.connectTls 跳转到标题

建立出站 TLS 连接。

该函数定义与 Deno 相同,但有以下限制:hostname 不能是 localhost 或空。

function Deno.connectTls(options: ConnectTlsOptions): Promise<Conn>

示例 跳转到标题

async function handler(_req) {
  // Make a TLS connection to example.com
  const connection = await Deno.connectTls({
    port: 443,
    hostname: "example.com",
  });

  // Send raw HTTP GET request.
  const request = new TextEncoder().encode(
    "GET / HTTP/1.1\nHost: example.com\r\n\r\n",
  );
  const _bytesWritten = await connection.write(request);

  // Read 15 bytes from the connection.
  const buffer = new Uint8Array(15);
  await connection.read(buffer);
  connection.close();

  // Return the bytes as plain text.
  return new Response(buffer, {
    headers: {
      "content-type": "text/plain;charset=utf-8",
    },
  });
}

Deno.serve(handler);

您找到所需内容了吗?

隐私政策