@grpc-web/middleware

中文文档
0.1.0发布于 2023/04/15MITTypeScriptCommonJS近 30 天下载 5.6万

gRPC Web middleware for Express and Connect.

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. License

@grpc-web/middleware npm tests coverage

gRPC Web middleware for Express and Connect.

For standalone proxy, see @grpc-web/proxy

Installation

npm install @grpc-web/middleware

Usage

This middleware proxies gRPC Web requests to the given gRPC server and can be used with existing Express and Connect applications. Here is a full working example of a proxy server created using express and cors packages:

const express = require('express');
const cors = require('cors');
const grpcWeb = require('@grpc-web/middleware');

const app = express();
app.use(cors({ exposedHeaders: ['grpc-status', 'grpc-message'] }));
app.use(grpcWeb('http://localhost:9090'));
app.listen(8080);

In above example, express can be replaced with connect. The address http://localhost:9090 refers to the gRPC server address to which gRPC Web requests are forwarded to and 8080 refers to the port on which the proxy server is running.

gRPC Web middleware only handles gRPC Web requests and ignores normal HTTP requests, so it can be used together with normal web applications:

app.use(grpcWeb('http://localhost:9090'));

app.get('/', (req, res) => {
  res.send('Hello World');
});

It can be run in the same process as the gRPC server:

// Express or Connect server
app.use(grpcWeb('http://localhost:9090'));
app.listen(8080);

// gRPC server
const server = new grpc.Server();
server.addProtoService(service, implementation);
server.bind('0.0.0.0:9090', credentials);
server.start();

It also accepts a custom function which can used for configuring the HTTP/2 connection to the gRPC server:

const http2 = require('node:http2');

app.use(
  grpcWeb(() => {
    return http2.connect('http://localhost:9090');
  })
);

License

MIT

版本与发布记录

@grpc-web/middleware 共发布 2 个版本,首次发布于 2023/04/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.0latest2023/04/153 年前
0.0.12023/04/153 年前

关于 @grpc-web/middleware 的常见问题

@grpc-web/middleware 是什么?

gRPC Web middleware for Express and Connect.

如何安装 @grpc-web/middleware?

在项目目录运行 npm install @grpc-web/middleware,或使用 pnpm add / yarn add / bun add。

@grpc-web/middleware 支持 TypeScript 吗?

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

@grpc-web/middleware 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@grpc-web/middleware 需要什么版本的 Node.js?

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

@grpc-web/middleware 可以商用吗?

@grpc-web/middleware 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@grpc-web/middleware 的下载量有多少?

根据 npm 官方下载统计,@grpc-web/middleware 在 2026/10/17 – 2026/11/15 这 30 天内被下载 55,905 次,日均约 1,864 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@grpc-web/middleware 还在维护吗?

最新版本 0.1.0 发布于 2023/04/15(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/04/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。