如何在浏览器上使用GRPC-Web?我的意思是,在纯浏览器代码中,不涉及任何NodeJS。
这里的官方例子:https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/helloworld主要是面向NodeJS的。
有没有办法在纯Javascript形式中使用GRPC-Web而不需要:
const {HelloRequest, HelloReply} = require('./helloworld_pb.js');
const {GreeterClient} = require('./helloworld_grpc_web_pb.js');意思是,仅仅是添加Javascript依赖项的标准<script>-way?并且能够做到:var client = new GreeterClient('http://localhost:8080');
发布于 2021-09-13 15:18:12
是。你需要把你的线人和webpack捆绑在一起。您提到的文档中也对此步骤进行了描述。在readme的底部
只需配置您的webpack来公开变量:
client.js
...
export function instantiateGreeterClient(...) {
return new GreeterClient(...)
};webpack.config.js
module.exports = {
...
entry: './path/to/client.js',
output: {
path: './bundle/js/',
filename: 'grpc.js',
library: {
name: 'grpc',
type: 'umd',
},
...
}然后像往常一样导入你的包。现在您可以在脚本标记代码中使用所有定义的变量,如下所示
<script src="path/to/grpc.js"></script>
<script>
const client = grpc.instantiateGreeterClient(...)
...
</script>有关更多信息,请访问webpack documentation。
https://stackoverflow.com/questions/66537085
复制相似问题