我怎样才能分离两个文件中的代码,并仍然在node.js中工作?

(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%工作。

代码如下:

我的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; 

现在我想分开这个代码在两个文件,路线和sendgrid ..例如:

ROUTE.JS:

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

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); }); 

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

在你的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. 创build一个新文件(也许像SendgridHandler.js
  2. 使该文件导出一个将路由器作为参数的函数
  3. 将附加到路由器的sendgrid处理程序的逻辑添加到该函数中
  4. require你的新模块,并将其传递给你的路由器

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

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

route.js

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

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;