我正在尝试通过protobuf.js使用proto消息,对它们进行编码,然后将它们发送到RabbitMQ消息代理。我的项目中有以下子文件夹:
- model
- protos
- transactions.proto
- RabitMQ.js
- routes
- rmq-api.js我在rmq-api.js文件中添加了一个执行以下操作(使用express)的路由:
const RabbitMQ = require('../model/RabbitMQ');
router.post('/api/transactions' ,function (req,res,next) {
RabbitMQ.PublishTransactionsMessage(DummyMessage).then(() => {
res.status(200).send({message: "OK :)"});
}).catch((e) => {
res.status(500).send({error:e.message});
});
});在RabitMQ.js文件中,我有以下代码:
module.exports = {
PublishTransactionsMessage: function(message) {
return new Promise((resolve, reject) => {
amqp.connect(RabbitMQConfig.url, function (error, connection) {
if (error) {
console.error("Could not connect to the rabbit message broker on {0} - " +
"Check connection please and try again".format(RabbitMQConfig.url));
console.error("Error message - {0}".format(error));
reject(error)
}
connection.createChannel(function(error, channel) {
if (error) {
console.error("Could Create channel - {0}".format(error.message));
reject(error)
}
const queue = RabbitMQConfig.queue;
channel.assertQueue(queue, {
durable: true
});
// Convert Message to protobuff
protobuf.load("./protos/transactions.proto").then((err, root) => {
if (err) {
reject(err);
}
let ScraperMessageResult = root.lookupType("transactions.ScraperMessageResult");
const errMsg = ScraperMessageResult.verify(message);
if (errMsg)
reject(errMsg);
let buffer = ScraperMessageResult.encode(message).finish();
channel.sendToQueue(queue, buffer);
console.log(`Sent ${message} to queue: ${queue}`);
resolve()
}).catch((err) => {
reject(err);
});
});
});
});
},
};在上面所示的代码中,代码行:protobuf.load("./protos/transactions.proto").then((err, root) => {,我不断捕捉到以下错误:

在这个catch块中:
}).catch((err) => {
reject(err);
});这似乎是一个非常简单的问题,但是我在网上没有找到任何东西,所以我可能在这里遗漏了一些非常简单的东西。另外,我尝试使用__dirname + "/protos/transaction.proto“,但仍然不能正常工作。请帮我弄清楚这件事。
发布于 2020-12-24 17:05:29
问题是您的函数不会查看dist目录,
await protocolBuffer.load(__dirname + '/item.proto'); should look there,此外,您还需要使用copy/cp命令大量复制文件,您可以将其添加为包json脚本的一部分,如下所示:
"build": "nest build && COPY src\\reading_list\\protocol_buffer\\*.proto dist\\reading_list\\protocol_buffer\\"https://stackoverflow.com/questions/58571525
复制相似问题