![](/img/trans.png)
[英]node.js mysql insert id - how do I expose the insert id outside of the insert query?
[英]How do I insert a transporter into services in an Node.js app?
我是一名新開發人員,正在為接受捐款的組織設置應用程序。
我試圖將其設置為在主任收到捐贈時收到通知的位置(理想情況下,我希望這既可以在她獲得新的捐贈時又可以在她有新的捐贈者簽名時進行)。
我得到了nodemailer部分,可以在微型測試應用程序上工作以正確設置它。 現在,我只需要將其插入應用程序中的正確位置即可。
現在,每次進行更改時都會觸發(我在index.js文件中擁有所有代碼)。 我與一位有關nodemailer的高級開發人員進行了交談,他提到了一個好處,就是您可以隨身攜帶運輸工具並將其插入任何需要的地方。
所以我想我可以將大部分的nodemailer代碼保留在index.js文件中,並將transporter部分放在需要的地方。 我一直在嘗試將其放置在服務文件中的不同位置,但是我顯然只是不理解流程。
這是我到目前為止的內容:
donationcontroller.js //create donation router.post('/createdonation', validate, function (req, res) { transporter.sendMail(HelperOptions, (error, info) => { if (error) { return console.log(error); } console.log("The donation was sent!"); console.log(info); }); });
donationServices.js //create donation exports.createDonation = function(req, res){ let newDonation = { used_clothing : 0, new_clothing : 0, used_shoes : 0, new_shoes : 0, baby_food: 0, diaper_bags: 0, bottles: 0, pacifiers: 0, diapers_boxes: 0, beds: 0, misc_items: 0 } newDonation[req.body.donationOption] = req.body.donationAmount return donation.create(newDonation) .then( function createSuccess(donation) { res.json ({ donation: donation }); }, function createError(err){ res.send(500, err.message); }, ); }
Index.js // nodemailer // houses the data to send in the email let transporter = nodemailer.createTransport({ service: 'gmail', // make true if using ssl certificate secure: false, // stmp port port: 25, auth: { user: 'test.test@gmail.com', pass: 'password' }, // protocol tls: { rejectUnauthorized: false } }); // use to construct body of email let HelperOptions = { from: '"Tester" <Test.test@gmail.com', to: 'test.test@gmail.com', subject: 'dd', text: 'dd' }; // contains callback data transporter.sendMail(HelperOptions, (error, info) => { if (error) { return console.log(error); } console.log("The donation was sent!"); console.log(info); });
編輯
當我第一次涉足這個問題時,我從根本上誤解了您的最初問題。 我應該澄清的是,您絕對不想在Angular代碼中使用nodemailer。 這僅應在您的后端(Node.js)中發生,因為這是唯一可以防止您的API密鑰或用戶名/密碼被窺視的地方。 只要將其放入前端應用程序,任何人都可以劫持您的gmail帳戶。 因此,將觸發電子郵件的觸發器留在后端,您的狀態會很好。 每當保存捐贈或進行任何捐贈時,只需以某種方式訪問您的后端API,即可調用sendEmail
函數。 :)
原始帖子
看來您可能想將該運輸工具放在自己的模塊中(我假設Index.js
是您的頂級入口點?),因此可以更輕松地將其導入任何文件。 嘗試這個:
sendEmail.js
let transporter = nodemailer.createTransport({
service: 'gmail',
// make true if using ssl certificate
secure: false,
// stmp port
port: 25,
auth: {
user: 'test.test@gmail.com',
pass: 'password'
},
// protocol
tls: {
rejectUnauthorized: false
}
});
module.exports = function ({from, to, subject, text}) {
// Promisify it, so you can easily chain it in the wild
return new Promise(function (resolve, reject) {
// use to construct body of email
let HelperOptions = {
from,
to,
subject,
text
};
// contains callback data
transporter.sendMail(HelperOptions, (error, info) => {
if (error) {
console.log(error);
return reject(error);
}
console.log("The donation was sent!");
console.log(info);
resolve(info);
});
});
};
然后,您可以在代碼中要發送電子郵件的任何地方使用此功能,如下所示:
donationServices.js
// import the email function
const sendEmail = require('./path/to/sendEmail.js')
// create donation
exports.createDonation = function(req, res){
let newDonation = {
used_clothing : 0,
new_clothing : 0,
used_shoes : 0,
new_shoes : 0,
baby_food: 0,
diaper_bags: 0,
bottles: 0,
pacifiers: 0,
diapers_boxes: 0,
beds: 0,
misc_items: 0
}
newDonation[req.body.donationOption] = req.body.donationAmount
return donation.create(newDonation)
.then(
function createSuccess(donation) {
sendEmail({
from: ‘test@gmail.com’,
to: ‘boss@gmail.com’,
subject: req.body.donationOption + ' donation alert'
text: 'yay donations!\n' + JSON.stringify(donation) // you get the point...
}).then(function (info) {
res.json({
donation: donation
});
}).catch(function (err) {
res.status(500).send(err);
// or maybe you don't care that the email failed:
// res.status(200).send({message: 'donation sent, but email didn\'t'});
});
},
function createError(err){
res.send(500, err.message);
}
);
}
希望這很清楚。 :)
PS我看到您可能正在使用用戶名/密碼進行nodemailer身份驗證。 根據個人經驗,我強烈建議您花一些時間來學習OAuth2等,並使用刷新令牌和整個工具來進行工作。 這確實值得,您將在此過程中學到很多東西。 編碼愉快!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.