user4378533
user4378533

Reputation:

How can I separate a code in two files, and still make it work in node.js?

(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:       "[email protected]",
                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:       "[email protected]",
                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?

Upvotes: 1

Views: 154

Answers (3)

hjl
hjl

Reputation: 2802

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:       "[email protected]",
        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;

Upvotes: 0

rdegges
rdegges

Reputation: 33864

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

var sendgrid = require('sendgrid');

module.exports.send = function(email) {
  sendgrid.send({
    to: email,
    from: '[email protected]',
    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 =)

Upvotes: 1

Jordan Foreman
Jordan Foreman

Reputation: 3888

  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:       "[email protected]",
            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);

Upvotes: 0

Related Questions