在访问本地主机3000时,我得到了在使用 Postman 之前无法获得/。
在postman中发送POST请求到本地主机的过程中,我的程序失败,并出现此错误
'节点:内部/进程/承诺:288触发未捕获异常(错误,真/* 来自承诺 */);^
错误:在TCP连接环绕器上连接ECONREFUSED::1:5672。连接后[作为完成时](节点:网络:1487:16){错误号:-61,代码:'ECONNREFUSED',系统调用:“连接”,地址:“::1”,端口:小行星5672
config.js
----------
module.exports = {
rabbitMQ: {
url: "amqp://localhost",
exchangeName: "logExchange",
},
};
Producer.js
-----------
const amqp = require("amqplib");
const config = require("./config");
//step 1 : Connect to the rabbitmq server
//step 2 : Create a new channel on that connection
//step 3 : Create the exchange
//step 4 : Publish the message to the exchange with a routing key
class Producer {
channel;
async createChannel() {
const connection = await amqp.connect(config.rabbitMQ.url);
this.channel = await connection.createChannel();
}
async publishMessage(routingKey, message) {
if (!this.channel) {
await this.createChannel();
}
const exchangeName = config.rabbitMQ.exchangeName;
await this.channel.assertExchange(exchangeName, "direct");
const logDetails = {
logType: routingKey,
message: message,
dateTime: new Date(),
};
await this.channel.publish(
exchangeName,
routingKey,
Buffer.from(JSON.stringify(logDetails))
);
console.log(
`The new ${routingKey} log is sent to exchange ${exchangeName}`
);
}
}
module.exports = Producer;
Server.js
----------
`const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const Producer = require("./producer");
const producer = new Producer();
app.use(bodyParser.json("application/json"));
app.post("/sendLog", async (req, res, next) => {
await producer.publishMessage(req.body.logType, req.body.message);
res.send();
});
app.listen(3000, () => {
console.log("Server started...");
});`
我是一个初级实习生广告是沿着一个youtube视频由https://www.youtube.com/watch?v=igaVS0S1hA4由Computerix,以帮助我理解的概念
我尝试重新启动rabbitMQ和express服务器并在不同的端口上运行它,但没有解决问题
1条答案
按热度按时间nx7onnlm1#
这是全局图
Postman -〉快递服务器-〉RabbitMQ-〉node.js
1 Postman 通过POST呼叫发送消息到Express,带有
Message Types
和payload
2 Express服务器通过
publish
将消息转发到Rabbit MQ3 RabbitMQ-将消息推送到自己的
message queue
中4每个
consumer
都从RabbitMQ接收到自己的消息。它之前已被装箱。有许多复杂的选项(顺序,优先级),但在这里谈论的基本和简单的模型。
步骤
我们需要遵循以下三个步骤。
1:安装
1.1 docker(运行RabbitMQ的简单容器方法)
1.2 node
1.3演示源
文件结构
1.4安装节点库
位于
RabbitMQ-Logger-Example\infoMS
目录位于
RabbitMQ-Logger-Example\loggerMS
目录位于
RabbitMQ-Logger-Example\warningAndErrorMS
目录1.5安装Postman
1.6安装Git Bash-我将使用git bash终端。它类似于Linux中的bash终端-如果你使用Linux,跳过这个。
2:绑定
2.1运行RabbitMQ
将此代码另存为
docker-compose.yml
运行它
如果Docker合成版本1.x
使用此URL打开浏览器
2.2运行express和所有node.js
位于
RabbitMQ-Logger-Example\infoMS
目录位于
RabbitMQ-Logger-Example\loggerMS
目录位于
RabbitMQ-Logger-Example\warningAndErrorMS
目录现在准备出发
3:消息
快跑 Postman ,
1制作自己的收藏
2提出三个要求
请求之一-
Send Info Message
1选择
POST
方法2输入网址
3单击
Body
选项卡4选择
Raw
单选按钮5在
body
中输入消息6选择
JSON
7按下
Send
按钮如果您看到
,您就成功了!
其他两个不同类型的消息内容相同,只是
logType
的值不同。Send Warning Message
请求对于
Warning
Send Error Message
请求对于
Error
这是真正的屏幕上的所有东西!
如果你浏览RabbitMQ的内容,你会发现很多东西。
这是
Info
通道信息的其中一个。如果要终止RabbitMQ
享受RabbitMQ!