(NODE.JS)
I have the following html form:
<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 .
I already made the code, and is working 100%.
Code bellow:
My 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:
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);
});
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?