post with json body

master
Matt Huntington 9 years ago
parent 46f61db54a
commit cf13dc8912

@ -17,6 +17,7 @@
}, },
"homepage": "https://github.com/mahuntington/emailer#readme", "homepage": "https://github.com/mahuntington/emailer#readme",
"dependencies": { "dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.3", "express": "^4.15.3",
"mailgun-js": "^0.11.2" "mailgun-js": "^0.11.2"
} }

@ -1,9 +1,12 @@
const express = require('express'); const express = require('express');
const app = 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){ app.use(bodyParser.json());
var mailgun = new Mailgun({apiKey: process.env.MAILGUNAPIKEY, domain: process.env.MAILGUNDOMAIN});
app.post('/', function(req, res){
var data = { var data = {
//Specify email 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! <a href="http://0.0.0.0:3030/validate?' + req.params.mail + '">Click here to add your email address to a mailing list</a>' html: 'Hello, This is not a plain-text email, I wanted to test some spicy Mailgun sauce in NodeJS! <a href="http://0.0.0.0:3030/validate?' + req.params.mail + '">Click here to add your email address to a mailing list</a>'
} }
//Invokes the method to send emails given the above data with the helper library console.log(req.body);
mailgun.messages().send(data, function (err, body) {
//If there is an error, render the error page mailgun.messages().send(req.body, function (err, body) {
if (err) { if (err) {
res.render('error', { error : err}); res.json({ error : err});
console.log("got an error: ", err); console.log("got an error: ", err);
} }
//Else we can greet and leave
else { else {
//Here "submitted.jade" is the view file for this landing page res.json({
//We pass the variable "email" from the url parameter in an object rendered by Jade success:200
res.send('submitted'); });
console.log(body); console.log(body);
} }
}); });

Loading…
Cancel
Save