@bufbuild/connect-node

中文文档
0.13.0发布于 2023/08/18Apache-2.0TypeScriptESM + CJS近 30 天下载 9.2万

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @bufbuild/connect brings type-safe APIs with Protobuf to TypeScript.

此软件包已被标记为弃用

Connect has moved to its own org @connectrpc and has a stable v1. Run `npx @connectrpc/connect-migrate@latest` to update. See https://github.com/connectrpc/connect-es/releases/tag/v0.13.1 for details.

原始使用文档
文档目录5 节
  1. createConnectTransport()
  2. createGrpcTransport()
  3. createGrpcWebTransport()
  4. connectNodeAdapter()
  5. Getting started

@bufbuild/connect-node

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @bufbuild/connect brings type-safe APIs with Protobuf to TypeScript.

@bufbuild/connect-node provides the following adapters for Node.js:

createConnectTransport()

Lets your clients running on Node.js talk to a server with the Connect protocol:

import { createPromiseClient } from "@bufbuild/connect";
+ import { createConnectTransport } from "@bufbuild/connect-node";
import { ElizaService } from "./gen/eliza_connect.js";

+ // A transport for clients using the Connect protocol with Node.js `http` module
+ const transport = createConnectTransport({
+   baseUrl: "https://demo.connectrpc.com",
+   httpVersion: "1.1"
+ });

const client = createPromiseClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "I feel happy." });
console.log(sentence) // you said: I feel happy.

createGrpcTransport()

Lets your clients running on Node.js talk to a server with the gRPC protocol:

import { createPromiseClient } from "@bufbuild/connect";
+ import { createGrpcTransport } from "@bufbuild/connect-node";
import { ElizaService } from "./gen/eliza_connect.js";

+ // A transport for clients using the gRPC protocol with Node.js `http2` module
+ const transport = createGrpcTransport({
+   baseUrl: "https://demo.connectrpc.com",
+   httpVersion: "2"
+ });

const client = createPromiseClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "I feel happy." });
console.log(sentence) // you said: I feel happy.

createGrpcWebTransport()

Lets your clients running on Node.js talk to a server with the gRPC-web protocol:

import { createPromiseClient } from "@bufbuild/connect";
+ import { createGrpcWebTransport } from "@bufbuild/connect-node";
import { ElizaService } from "./gen/eliza_connect.js";

+ // A transport for clients using the Connect protocol with Node.js `http` module
+ const transport = createGrpcWebTransport({
+   baseUrl: "https://demo.connectrpc.com",
+   httpVersion: "1.1"
+ });

const client = createPromiseClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "I feel happy." });
console.log(sentence) // you said: I feel happy.

connectNodeAdapter()

Run your Connect RPCs on the Node.js http, https, or http2 modules.

// connect.ts
import { ConnectRouter } from "@bufbuild/connect";

export default function(router: ConnectRouter) {
  // implement rpc Say(SayRequest) returns (SayResponse)
  router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({
    sentence: `you said: ${req.sentence}`,
  }));
}
// server.ts
import * as http2 from "http2";
+ import routes from "connect";
+ import { connectNodeAdapter } from "@bufbuild/connect-node";

http2.createServer(
+ connectNodeAdapter({ routes }) // responds with 404 for other requests
).listen(8080);

With that server running, you can make requests with any gRPC, gRPC-Web, or Connect client.

buf curl with the gRPC protocol:

buf curl --schema buf.build/connectrpc/eliza \
  --protocol grpc --http2-prior-knowledge \
  -d '{"sentence": "I feel happy."}' \
  http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say

curl with the Connect protocol:

curl \
    --header "Content-Type: application/json" \
    --data '{"sentence": "I feel happy."}' \
     --http2-prior-knowledge \
    http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say

Node.js with the gRPC protocol:

import { createPromiseClient } from "@bufbuild/connect";
import { createGrpcTransport } from "@bufbuild/connect-node";
import { ElizaService } from "./gen/eliza_connect.js";

const transport = createGrpcTransport({
  baseUrl: "http://localhost:8080",
  httpVersion: "2",
});

const client = createPromiseClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "I feel happy." });
console.log(sentence) // you said: I feel happy.

A client for the web browser actually looks identical to this example - it would simply use createConnectTransport from @bufbuild/connect-web instead.

Getting started

To get started with Connect, head over to the docs for a tutorial, or take a look at our example.

版本与发布记录

@bufbuild/connect-node 共发布 16 个版本,首次发布于 2023/02/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.13.0latest2023/08/183 年前
0.12.02023/07/253 年前
0.11.02023/06/293 年前
0.10.12023/06/213 年前
0.10.02023/06/153 年前
0.9.12023/05/313 年前

关于 @bufbuild/connect-node 的常见问题

@bufbuild/connect-node 是什么?

Connect is a family of libraries for building and consuming APIs on different languages and platforms, and @bufbuild/connect brings type-safe APIs with Protobuf to TypeScript.注意:该包已被作者标记为弃用——Connect has moved to its own org @connectrpc and has a stable v1. Run `npx @connectrpc/connect-migrate@latest` to update. See https://github.com/connectrpc/connect-es/releases/tag/v0.13.1 for details.

如何安装 @bufbuild/connect-node?

在项目目录运行 npm install @bufbuild/connect-node,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bufbuild/protobuf(^1.2.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bufbuild/connect-node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bufbuild/connect-node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bufbuild/connect-node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bufbuild/connect-node 可以商用吗?

@bufbuild/connect-node 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bufbuild/connect-node 的下载量有多少?

根据 npm 官方下载统计,@bufbuild/connect-node 在 2026/10/09 – 2026/11/07 这 30 天内被下载 91,925 次,日均约 3,064 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bufbuild/connect-node 还在维护吗?

最新版本 0.13.0 发布于 2023/08/18(3 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/04。作者已将其标记为弃用,新项目建议寻找替代方案。