简体   繁体   English

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

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

(NODE.JS) (Node.js的)

I have the following html form: 我有以下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>

And i want send a confirmation message, for this i'm using the Sendgrid - https://sendgrid.com . 我想发送一条确认消息,为此,我正在使用Sendgrid- https: //sendgrid.com。

I already made the code, and is working 100%. 我已经编写了代码,并且可以100%工作。

Code bellow: 代码如下:

My route.js 我的route.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;

Now i want separete this code in two files, the route, and the sendgrid.. for example: 现在我想将此代码分成两个文件,即路由和sendgrid ..例如:

ROUTE.JS: ROUTE.JS:

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

sendGrid.js sendGrid.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);
    });

I dont know how to do that, i need this to my personal organization, i hate this code mess in my application, and also for maintenance. 我不知道该怎么做,我需要这个信息给我的个人组织,我讨厌我的应用程序中的这些代码混乱,也讨厌维护。 Somebody, please? 有人吗

In your sendGrid.js file, define the following helper function: 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);
    }
  });
};

Then, in your routes.js , import and use your sendGrid.js module like so: 然后,在您的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
});

In Node, it's quite easy to 'modularize' your code by defining export functions =) 在Node中,很容易通过定义导出函数来“模块化”代码=)

  1. Make a new file (maybe something like SendgridHandler.js ) 制作一个新文件(可能类似于SendgridHandler.js
  2. Make that file export a function that takes a router as a parameter 使该文件导出以路由器为参数的函数
  3. Add the logic that attached the sendgrid handler to the router into that function 在该函数中添加将sendgrid处理程序附加到路由器的逻辑
  4. require your new module, and pass it your router require您的新模块,并将其传递给路由器

SendgridHandler.js SendgridHandler.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);
        });
    });
};

Index.js Index.js

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

route.js route.js

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

sendGrid.js sendGrid.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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Node.js 制作应用程序,它可以分隔所有具有相同扩展名的文件? - How can i make an application using Node.js, which can separate all the files with same extension? 当我将它分成两个文件时,Node.js https 服务器不起作用 - Node.js https server doesn't work when I separate it in two files 如何在不显示API密钥的情况下使此代码正常工作? 我在Node.js / Angular / Express上构建为应用程序 - How can I make this code work without showing my API keys? I am building as app on Node.js/Angular/Express 如何让javascript代码与node.js一起工作? - How to make javascript code to work witn node.js? 如何在Node.js Express应用程序中使脚本文件可用于客户端? - How can I make script files available to the client in a Node.js Express app? Node.js:组织我的应用程序:在单独的文件中使用路由和模型,如何从路由中获取模型? - Node.js : organising my application : using routes and models in separate files, how can I get models from routes? 关于node.js的Javascript:如何使代码将SQL值存储在数组中 - Javascript, regarding node.js: How can I make the code store, in an array, SQL values 如何使用 Node.JS 将文件移动到目录? - How can I move files to a directory using Node.JS? 如何返回目录文件? (JavaScript,Node.js) - How can I return the files of a directory? (JavaScript, Node.js) 如何在 node.js 中使此请求同步 - How can I make this request synchronous in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM