Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在RabbitMQ队列上发布的模块导出函数_Javascript_Node.js_Rabbitmq - Fatal编程技术网

Javascript 在RabbitMQ队列上发布的模块导出函数

Javascript 在RabbitMQ队列上发布的模块导出函数,javascript,node.js,rabbitmq,Javascript,Node.js,Rabbitmq,我有一个nodeJS服务器,想设置一个连接和导出功能,将消息从js文件发送到队列 const amqp=require(“amqplib”); const url=process.env.RABBITMQ_服务器; 让通道=空; amqp.connect(url,(e,conn)=> conn.createChannel((e,ch)=>{ 信道=ch; }) ); module.exports=publishToQueue=( 数据, queueName=process.env.RABBITM

我有一个nodeJS服务器,想设置一个连接和导出功能,将消息从js文件发送到队列

const amqp=require(“amqplib”);
const url=process.env.RABBITMQ_服务器;
让通道=空;
amqp.connect(url,(e,conn)=>
conn.createChannel((e,ch)=>{
信道=ch;
})
);
module.exports=publishToQueue=(
数据,
queueName=process.env.RABBITMQ_队列
)=>channel.sendToQueue(queueName,new Buffer.from(data));
process.on(“退出”,代码=>{
ch.close();
log(“关闭rabbitmq通道”);
});
但是当我尝试导入并使用它时,我得到了空对象{}

挂钩:{
beforeCreate(实例){
控制台日志(amqp)
amqp(JSON.stringify(instance.toJSON()))
}
}
更新: 感谢HosseinAbha的回答,我最终在Constructor中创建了一个类并建立了连接

const amqp = require("amqplib");
const url = process.env.RABBITMQ_SERVER;

class RABBITMQ {
    constructor () {
        this.connection = null
        this.channel = null
        this.connect()
    }
    async connect () {
        try {
            this.connection = await amqp.connect(url)
            this.channel = await this.connection.createChannel()
            await this.channel.assertQueue(process.env.RABBITMQ_QUEUE)
            await this.channel.bindQueue(process.env.RABBITMQ_QUEUE, process.env.RABBITMQ_EXCHANGE)
            await this.channel.assertExchange(process.env.RABBITMQ_EXCHANGE, 'fanout', { durable: true })
        } catch (err){
            console.log(err)
            throw new Error('Connection failed')
        }
    }
    async postData (data) {
        if (!this.connection) await this.connect()
        try {
            this.channel.publish(process.env.RABBITMQ_EXCHANGE, `${data.type}.${data.event_type}`, new Buffer.from(JSON.stringify(data)))
        } catch (err){
            console.error(err) 
        }
    }
}
module.exports = new RABBITMQ()

您的
publishToQueue
函数应该返回一个承诺,并且应该在执行任何操作之前连接到rabbitMQ。您的函数应该是这样的:

const connectToChannel = async () => {
  try {
    let connection = await amqp.connect(url)
    return connection.createChannel()
  } catch (e) {
    console.error('failed to create amqp channel: ', e)
  }
}

let channel;
module.exports = publishToQueue = async (
 data,
 queueName = process.env.RABBITMQ_QUEUE
) => {
  if (channel == null) {
     channel = await connectToChannel();
  }
  return channel.sendToQueue(queueName, Buffer.from(data));
}

您也不需要实例化缓冲区和
缓冲区。from
就足够了。

在创建
方法之前
钩子和
是什么?您使用的是什么框架/库?如何导入第一个模块?