如何通过nodeJs执行NestJs TCP微服务命令
现在我正在使用普通的nodeJs 来处理 Lambda (AWS),该 lambda 包含一个还需要向 MS 发送命令的函数。我尝试使用 net
创建一个套接字来发送命令(字符串化),但它不会触发任何内容。 我的示例nodeJs代码:
const net = require('net');
const saveProducts = (products) => {
let socket = new net.Socket();
socket.setEncoding('UTF8');
socket.on('data', function (data) {
console.log('ON DATA'); // print out data
console.log(data.toString()); // print out data
});
socket.connect(options.port, options.host, function () {
//called when connection is created
const command = JSON.stringify({ pattern: { cmd: 'update-many', ctrl: 'product' } });
socket.write(command, 'UTF8', function (err) {
console.log(err);
});
});
}
我使用网络嗅探器来获取示例消息结构..
类似的问题,但建议只是添加@nestjs/microservices
,我想知道如果没有它该怎么做。
I have a working Microservice(MS) based on https://docs.nestjs.com/microservices/basics using a TCP protocol. Executing a command from my NestJS API was easy by implementing the @nestjs/microservices
Client.
Now im working on a Lambda (AWS) in plain nodeJs, this lambda contains a function that also need to send a command to the MS. I tried using net
to create a Socket to send a command (stringified) but it doesn't trigger anything.
my example nodeJs code:
const net = require('net');
const saveProducts = (products) => {
let socket = new net.Socket();
socket.setEncoding('UTF8');
socket.on('data', function (data) {
console.log('ON DATA'); // print out data
console.log(data.toString()); // print out data
});
socket.connect(options.port, options.host, function () {
//called when connection is created
const command = JSON.stringify({ pattern: { cmd: 'update-many', ctrl: 'product' } });
socket.write(command, 'UTF8', function (err) {
console.log(err);
});
});
}
I have used a network sniffer to get an example message structure..
similar issue but the suggestion is only to add @nestjs/microservices
, I was wondering how to do it without it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过长时间的研究,发现您需要发送的模式是什么:
[MSG_LEN]#{pattern: "[PATTERN_STRING]", data: "[DATA]", id: "[ID]" }
示例:
62#{"pattern":"find","id":"ce51ebd3-32b1-4ae6-b7ef-e018126c4cc4"}
参数
id
用于@ MessagePattern
,没有它@EventPattern
将会被触发。After some long research found out what the pattern is what you need to send:
[MSG_LEN]#{ pattern: "[PATTERN_STRING]", data: "[DATA]", id: "[ID]" }
Example:
62#{"pattern":"find","id":"ce51ebd3-32b1-4ae6-b7ef-e018126c4cc4"}
The parameter
id
is for@MessagePattern
, without it@EventPattern
will be triggered.