[英]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中,很容易通过定义导出函数来“模块化”代码=)
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.