【问题标题】:Delegate route handlers to other modules in NodeJS+Express将路由处理程序委托给 NodeJS+Express 中的其他模块
【发布时间】:2015-03-23 15:09:19
【问题描述】:

我一直在努力避免在我的 routes.js 文件中产生开销。

这里是:

module.exports = function(app, db) {
app.get('/', function(req, res) {
    res.render('index')
});

app.get('/contact-us', function(req, res) {
    var col = db.collection('contacts');

    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'gmail.user@gmail.com',
            pass: 'userpass'
        }
    });
});
});
}

如您所见,仅通过实例化 mongo 收集和邮件传输器就已经被业务逻辑淹没了。我找不到任何关于如何将此逻辑委托给外部模块的材料,例如 sendmail.js、savetomongo.js 等。

有什么建议吗?

【问题讨论】:

    标签: javascript node.js mongodb express


    【解决方案1】:

    我做了一些修改。我已根据您的要求进行了更新。 你需要根据你的实际需要来制作。

    var sendmail = require('./sendmail.js');
    var savetomongo = require('./savetomongo.js');
    module.exports = function(app, db) {
        app.get('/', function(req, res) {
            res.render('index')
        });
    
        app.get('/contact-us', function(req, res) {
            var col = db.collection('contacts');
            var document = {'id': 'xyz'};
            savetomongo.save(col, document, function(error, is_save) {
                if (error) {
                    //handle error
                } else {
                    // next()
                    sendmail.sendEmail('DUMMY <from@xyz.com>', 'to@xyz.com', 'TestEmail', 'Only for testing purpose', function(error, isSend) {
                        if (error) {
                            //handle error
                        } else {
                            // next() 
                            //res.render('index')
                        }
                    });
                }
            });
    
        });
    }
    

    //sendmail.js

    module.exports = {
        sendEmail: function(fromEmailFormatted, toEmail, subject, message, fn) {
            var mailOptions = {
                from: fromEmailFormatted, // sender address
                to: toEmail, // list of receivers
                subject: subject, // Subject line
                html: message // html body
            };
            var transporter = nodemailer.createTransport({
                service: 'Gmail',
                auth: {
                    user: 'gmail.user@gmail.com',
                    pass: 'userpass'
                }
            });
            // send mail with defined transport object
            transporter.sendMail(mailOptions, function(error, info) {
                if (error) {
                    return fn(error);
                } else {
                    return fn(null, true);
                }
            });
        }
    }
    

    //savetomongo.js

    module.exports = {
        save: function(col, data, fn) {
            col.insert(data, {w: 1}, function(err, records) {
                if (err) {
                    return fn(err);
                } else {
                    return fn(null, records);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多