- Hola, soy nuevo en nodejs por favor guíame. esta es la clase main.ts que tiene una declaración para invocar OnEvent('send_email') en la clase de controlador, también puede ver la clase de controlador.
- lo agregué
async function bootstrap() { const myEmitter = new EventEmitter2(); const app = await NestFactory.createMicroservice( AppModule, appConfig.microservice, ); //#region RabbitMQ Implementation const email = { email: { from: 'GeoNoon <xzy@yahoo.com>', to: 'ali.whu@yahoo.com', subject: 'Hello ✔', html: '<b>Hello world? 测试邮件</b>', }, }; amqplib.connect( 'amqp://localhost', (error0, connection) => { if (error0) { throw error0; } connection.createChannel((error1, channel) => { if (error1) { throw error1; } const queue = 'Test_emails_queue'; channel.assertQueue(queue, { durable: false }); channel.sendToQueue(queue, Buffer.from(JSON.stringify(email))); console.log(' [*] Waiting for message', queue); // myEmitter.on('send_email', function(email:{}) { // console.log(this.event, email); // }); const response = myEmitter.emit('send_email', JSON.stringify(email)); console.log(response); }); }, ); //#endregion await app.listen(); console.log('Geonoon Mail Microservice is listening'); } bootstrap();
- esta es la clase app.controller que tiene un método para enviar un correo electrónico al cliente.
- por favor ayúdame como puedo hacer esto. Gracias
export class AppController { constructor(private readonly appService: AppService) {} @OnEvent('send_email') async handlerEmailSend(data: Record<string, any>) { console.log(data); if ( data.email && data.email.from && data.email.to && data.email.subject && (data.email.html || data.email.text) ) { return await this.appService.sendEmail(data as EmailContent); } else { Logger.warn('电子邮件数据格式错误'); } } }