简体   繁体   English

gRPC - Node Js - 如何使用不同端口将请求从客户端发送到服务器?

[英]gRPC - Node Js - How to send request from client to server with different port?

I'm a new in gRPC.我是 gRPC 的新手。 I code my app with gRPC.我使用 gRPC 编写我的应用程序。 It's working good with client and server in same port.它在同一端口与客户端和服务器一起工作良好。 Now, I want to set up the client port differently from the port on the server.现在,我想设置与服务器端口不同的客户端端口。 I'm find many hours this problem in Google but I cant find it.我在谷歌上发现了很多小时这个问题,但我找不到它。 What can I do.我能做什么。

My server code:我的服务器代码:

const grpc = require('grpc')

const protoLoader = require('@grpc/proto-loader')
const packageDefinition = protoLoader.loadSync('notes.proto');
const notesProto = grpc.loadPackageDefinition(packageDefinition);

const uuidv1 = require('uuid/v1')

const server = new grpc.Server()
const notes = [
    { id: '1', title: 'Note 1', content: 'Content 1'},
    { id: '2', title: 'Note 2', content: 'Content 2'}
]

server.addService(notesProto.NoteService.service, {
  ...
})

server.bind('127.0.0.1:50051',
  grpc.ServerCredentials.createInsecure())
console.log('Server running at http://127.0.0.1:50051')
server.start()

My client code:我的客户代码:

const PROTO_PATH = '../notes.proto';

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader')
const packageDefinition = protoLoader.loadSync(PROTO_PATH);
const NoteService = grpc.loadPackageDefinition(packageDefinition).NoteService

const client = new NoteService('localhost:50052',  // <- problem here
    grpc.credentials.createInsecure());

module.exports = client

The client does not have its own port.客户端没有自己的端口。 That address gives the port number where the client will attempt to connect to a server.该地址给出了客户端将尝试连接到服务器的端口号。 The two numbers need to match or the client will be unable to find the server.这两个数字需要匹配,否则客户端将无法找到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点Js和Angular Js如何将请求从服务器发送到客户端 - Node Js and Angular Js how to send request from server to client 使用Node.js从客户端向服务器发送json请求 - use Node.js to send a json request from client to the server 如何从客户端将 HTML 文档元素发送到 Node JS 中的服务器? - How to send HTML document element to server in Node JS from client? 从 node.js 服务器向客户端发送 axios post 请求错误消息 - Send axios post request error message from node js server to client side 如何在节点中从服务器向客户端发送数据? - How to send data from server to client in node? 从客户端向node.js发送发布请求 - Send post request from client to node.js 从Node.js中的不同IP发送HTTP请求 - Send HTTP request from different IPs in Node.js 从Angular 2客户端到Node.js服务器的HTTP POST请求 - HTTP POST request from Angular 2 client to Node.js server 如何使用 Pusher 将用户的存款和取款详细信息从客户端发送到 Node.js(快递)服务器? - How to send deposit and withdraw details of user from client to Node.js (express) server using Pusher? 如何从 promise 中提取值并通过 node.js 服务器将它们发送到客户端? - How can i extract values from promise and send them to client via node.js server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM