如何将代码分成两个文件,并仍然使其在node.js中工作

How can I separate a code in two files, and still make it work in node.js?

本文关键字:工作 js node 文件 代码 两个      更新时间:2023-09-26

(NODE.JS(

我有以下 html 表单:

<form class="options-form" role="form" id="form" method="post" action="/">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <label for="email">Email:</label>
        <input type="text" id="email" name="email">
</form>

我想发送一条确认消息,为此我正在使用 Sendgrid - https://sendgrid.com .

我已经编写了代码,并且正在 100% 工作。

代码波纹管:

我的路线.js

var express = require('express');
var router = express.Router();
var auth = require('../authentication/sendgrid');
var sendgrid = require('sendgrid')(auth.sg.username, auth.sg.password);

 router.get('/', function(req, res) {
   res.render('index');
 });
  router.post('/', function(req, res) {
     sendgrid.send({
                to:         req.body.email,
                from:       "confirmation@mycompany.com",
                subject:    "Confirmation email"
                html:       "some html for the body email",
        },
        function(err, json) {
            if (err) {
                return console.error(err);
            }
            console.log(json);
    });
 });
module.exports = router;

现在我想将此代码分成两个文件,路由和发送网格..例如:

路线.JS:

router.post('/', function(req, res) {
    something here that make the sendgrid send the email.
});

发送网格.js

     sendgrid.send({
                to:         req.body.email,
                from:       "confirmation@mycompany.com",
                subject:    "Confirmation email"
                html:       "some html for the body email",
        },
        function(err, json) {
            if (err) {
                return console.error(err);
            }
            console.log(json);
    });

我不知道该怎么做,我需要这个到我的个人组织,我讨厌我的应用程序中的这种代码混乱,也用于维护。有人吗?

sendGrid.js文件中,定义以下帮助程序函数:

var sendgrid = require('sendgrid');
module.exports.send = function(email) {
  sendgrid.send({
    to: email,
    from: 'confirmation@mycompany.com',
    subject: 'confirmation email',
    html: 'some html',
  }, function(err, json) {
    if (err) {
      return console.error(err);
    } else {
      console.log(json);
    }
  });
};

然后,在您的routes.js中,导入并使用您的sendGrid.js模块,如下所示:

var express = require('express');
var sendGrid = require('./sendGrid');
var router = express.Router();
router.post('/', function(req, res) {
  sendGrid.send(req.body.email);  // this is a call to your helper function defined in the other file
});

在 Node 中,通过定义导出函数来"模块化"代码非常容易 =(

  1. 创建一个新文件(也许类似于SendgridHandler.js(
  2. 使该文件导出成为以路由器为参数的函数
  3. 将将发送网格处理程序附加到路由器的逻辑添加到该函数中
  4. require您的新模块,并将其传递给您的路由器

发送网格处理程序.js

module.exports = function(router) {
    router.post('/', function(req, res){
        sendgrid.send({
            to:         req.body.email,
            from:       "confirmation@mycompany.com",
            subject:    "Confirmation email"
            html:       "some html for the body email",
        }, function(err, json) {
            if (err) {
                return console.error(err);
            }
            console.log(json);
        });
    });
};

索引.js

var router = express.Router();
...
var SendgridHandler = require('./SendgridHandler');
SendgridHandler(router);

路由.js

var sg = require('sendGrid.js');
router.post('/', function(req, res) {
    sg.send(req, res);
});

发送网格.js

var auth = require('../authentication/sendgrid');
var sendgrid = require('sendgrid')(auth.sg.username, auth.sg.password);
var sg = {
    send: send;
}
function send(req, res) {
    sendgrid.send({
        to:         req.body.email,
        from:       "confirmation@mycompany.com",
        subject:    "Confirmation email"
        html:       "some html for the body email",
    },
    function(err, json) {
        if (err) {
            return console.error(err);
        }
        console.log(json);
    });
}
module.exports = sg;