- Hi, i am new in nodejs please guid me. this is main.ts class which have statement to invoke OnEvent('send_email') in controller class you can see controller class also.
- i added it
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();
- this is app.controller class which has method to send email to customer.
- please help me how can i do this. thanks
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('电子邮件数据格式错误');
}
}
}