diff --git a/package.json b/package.json index a159090..c075599 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/mahuntington/emailer#readme", "dependencies": { + "body-parser": "^1.17.2", "express": "^4.15.3", "mailgun-js": "^0.11.2" } diff --git a/server.js b/server.js index 8c89ecd..4437772 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,12 @@ const express = require('express'); const app = express(); -var Mailgun = require('mailgun-js'); +const Mailgun = require('mailgun-js'); +const mailgun = new Mailgun({apiKey: process.env.MAILGUNAPIKEY, domain: process.env.MAILGUNDOMAIN}); +const bodyParser = require('body-parser'); -app.get('/', function(req, res){ - var mailgun = new Mailgun({apiKey: process.env.MAILGUNAPIKEY, domain: process.env.MAILGUNDOMAIN}); +app.use(bodyParser.json()); + +app.post('/', function(req, res){ var data = { //Specify email data @@ -15,18 +18,17 @@ app.get('/', function(req, res){ html: 'Hello, This is not a plain-text email, I wanted to test some spicy Mailgun sauce in NodeJS! Click here to add your email address to a mailing list' } - //Invokes the method to send emails given the above data with the helper library - mailgun.messages().send(data, function (err, body) { - //If there is an error, render the error page + console.log(req.body); + + mailgun.messages().send(req.body, function (err, body) { if (err) { - res.render('error', { error : err}); + res.json({ error : err}); console.log("got an error: ", err); } - //Else we can greet and leave else { - //Here "submitted.jade" is the view file for this landing page - //We pass the variable "email" from the url parameter in an object rendered by Jade - res.send('submitted'); + res.json({ + success:200 + }); console.log(body); } });