如何将代码分成两个文件,并仍然使其在node.js中工作
How can I separate a code in two files, and still make it work in 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% 工作。
代码波纹管:
我的路线.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 中,通过定义导出函数来"模块化"代码非常容易 =(
- 创建一个新文件(也许类似于SendgridHandler.js(
- 使该文件导出成为以路由器为参数的函数
- 将将发送网格处理程序附加到路由器的逻辑添加到该函数中
-
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;
相关文章:
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- JS可以在Chrome中工作,但不能在Firefox中工作
- JS,用于播放提示音以通知未按预期工作
- JS在firefox中无法正常工作
- 为什么不是'我的JS滑块正在工作
- 谷歌地图Api和JS代码不工作
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 当加载几个js文件时,defer属性应该如何工作
- 这和javascript中的闭包(在纸上工作.js)
- 嘶嘶声中的正则表达式如何工作.js
- 第一个现实生活-工作JS脚本.我怎样才能使它更有效率
- 简单事件侦听器不工作-JS
- 为什么不't工作js调试智能
- 字符串模板不工作- js
- 多维数组indexOf不工作js
- 原型“this"不工作(JS)
- 不能让Autobahn的例子工作(js和python)
- 为什么这个选择器不能工作?JS
- 为什么这个脚本不能工作?(JS)
- Spring MVC 和 Bootstrap - css,img 工作.js不工作