Reputation: 2079
I have a mail service class set up on an express server.
Which method of creating a transport is recommended?
class mailService {
private transport: nodemailer.Transport;
constructor(){
this.transport = nodemailer.createTransport('configstring');
}
public sendEmail(email: string){
//send email
}
}
OR
class mailService {
public sendEmail(email: string){
let transporter = nodemailer.createTransport('configstring');
//send email
}
public sendOtherEmail(email: string){
let transporter = nodemailer.createTransport('configstring');
//send email
}
}
The documentation say "You can reuse a transport as often as you like after creating it" Which leads me to think that the first option would be better, however can't tell if there would be any advantage.
Would simply creating the transport every time be an issue of repetition or will there be multiple instances floating around in memory, orphaned every time the sendEmail
function is executed?
Upvotes: 2
Views: 1210
Reputation: 2597
There is little advantage in using the second method. It might come in handy if you want to change the transport configuration between different sending jobs.
If that is not the case, it is recommended to stick to using a single transport (1st method) for sending emails according to the DRY principle.
You also shouldn't be concerned about memory here because Node has a garbage collector and memory will be freed after your sendOtherEmail()
function ends.
Upvotes: 3