纯 nodejs 项目如何调用 nestjs 微服务?-灵析社区

劳资最帅

我们知道 nestjs http 服务器调用 nestjs 微服务是简单的,已经封装好了, 那么脱离nestjs 环境,普通的 nodejs 环境 如何调用微服务呢?

阅读量:138

点赞量:0

问AI
从 stackoverflow 找到一个答案."https://stackoverflow.com/questions/55942795/call-my-nestjs-microservice-with-nodejs-app" (https://link.segmentfault.com/?enc=JTF6I%2B%2BG5CGb73o%2BPx4yhg%3D%3D.D9hR6%2FppgAruZInVSRjBvE38XFjh3UmpFrdiX38rNzkMQI4F%2FhGLfUZYuThwqD9ISmExZCkEwZN3qgipAq3bwkR46v2PQs6566ayaLfyVkNCuVXdvFbzuaZb9Ep8qYzz) import { ClientTCP } from "@nestjs/microservices"; import { lastValueFrom } from "rxjs"; async function bootstrap() { const client = new ClientTCP({ host: "localhost", port: 3001, }); await client.connect(); const pattern = { cmd: "math:wordcount" }; const data = "12314"; const result = await lastValueFrom(client.send("math:wordcount", data)).catch( (e) => { console.log(`e`, e); } ); console.log(result); } bootstrap(); 当然安装的话需要安装这些包,不然会缺少依赖...: "@nestjs/common": "^10.3.0", "@nestjs/core": "^10.3.0", "@nestjs/microservices": "^10.3.0"