I am trying to send a test email using node.
My server code index.js looks like this:
var http = require("http"),
express = require('express'),
nodemailer = require('nodemailer'),
bodyParser = require('body-parser'),
app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.post('/contact', function (req, res) {
var name = req.body.name;
var email = req.body.email;
var message = req.body.message;
var mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: "email@gmail.com",
pass: "password"
}
});
mailOpts = {
from: name + ' <' + email + '>', //grab form data from the request body object
to: 'cmatsoukis@gmail.com',
subject: "Website Contact",
text: message
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
res.send(false);
}
//Yay!! Email sent
else {
res.send(true);
}
});
});
app.listen(1337, '127.0.0.2');
This is my ajax code when I submit the form
var form = $("form#contact_form");
form.submit(function () {
event.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
var msg = $('#message').val();
var info = {"name" : name, "email": email, "message" : msg}
$.ajax({
url: 'http://127.0.0.2:1337',
type: 'POST',
data: JSON.stringify(info),
contentType: "application/json; charset=utf-8",
jsonpCallback: 'callback', // this is not relevant to the POST anymore
//dataType: 'json',
success: function (data) {
MailSuccess()
lightSpeed();
},
error: function () {
MailFail();
}
});
});
I am getting the error POST http://127.0.0.2:1337/ 404 (Not Found)
I do not think I have the url correct. I have app.post('/contact', function (req, res) {
But I do not this that is correct. And shouldn't I put a file name in the url: 'http://127.0.0.2:1337',
?
I believe I have everything else correct. Please let me know.