1

(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?

3 Answers3

1

In your sendGrid.js file, define the following helper function:

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:

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 =)

rdegges
  • 32,786
  • 20
  • 85
  • 109
  • 1
    Thanks @rdegges.. your answer is perfect!!! I have other code inside my router.post and now i can use the same method for two things! Thanks very much –  Dec 19 '14 at 16:54
  • Happy to help. Have fun with your project! – rdegges Dec 19 '14 at 16:54
0
  1. Make a new file (maybe something like 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
  4. require your new module, and pass it your router

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);
Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65
  • I'll also suggest you check out another answer of mine to get a better understanding of how modularity works with node.js apps - http://stackoverflow.com/questions/21831119/want-to-get-crystal-clear-about-nodejs-app-structure-full-javascript-stack/21832305#21832305 – Jordan Foreman Dec 19 '14 at 16:40
  • Thanks very much for your help Jordan. Your answer is good, but i need leave the router.post in my routes.js, because i have other codes inside.. i will check your other answer. –  Dec 19 '14 at 16:52
  • Glad I could help a little. It sounds like the other two answers here are better fits for your desired pattern, so definitely take one of those approaches. Once you have a better understanding of how to use module.exports and require, you'll be able to use all kinds of patterns throughout your apps! Good luck :) – Jordan Foreman Dec 19 '14 at 16:56
0

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;
hjl
  • 2,794
  • 3
  • 18
  • 26