I am writing a program which will send a mail on clicking a button in html using nodemailer. My Application server is running on 8383 port and the node server is running on 8080. I am getting an error "POST
http://127.0.0.1:8080/Webcontent/api/mail404 (Not Found)127.0.0.1:8080/Webcontent/api/mail:1 Error: Cannot POST /Webcontent/api/mail"
Kindly look into the code and suggest a solution.
server.js file
var express = require('express'),
cors = require('cors');
var app = express();
app.use(cors());
var port = process.env.PORT || 8080;
var database = require('./config/database');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
app.use(express.static(__dirname + '/public'));
app.use(morgan());
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(methodOverride('X-HTTP-Method-Override'));
require('./app/routes.js')(app);
app.listen(port);
console.log("App listening on port " + port);
routes.js file
var User = require('./models/user');
var Mail = require('./models/mail');
module.exports = function(app) {
app.post('http://127.0.0.1:8080/Webcontent/api/mail ', function(req, res) {
console.log('inside post');
Mail.fire(req.body.text1);
console.log(req.body.text1);
});
app.get('*', function(req, res) {
console.log('fff');
res.sendFile('index.html', { root: path.join(__dirname, './public') });
});
};
mail.js file
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail", // sets automatically host, port and connection security settings
auth: {
user: "test@gmail.com",
pass: "Test"
}
});
exports.fire= function fire(username){
smtpTransport.sendMail({ //email options
from: "test@gmail.com", // sender address. Must be the same as authenticated user if using GMail.
to: "receive@yahoo.com", // receiver
subject: "mail using nodemailer", // subject
text: "mail body text" // body
}, function(error, response){ //callback
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});
};
controller.js file// used to call the function(sendmail) on button click
$scope.sendMail = function() {
//alert("inside createtodo");
$http.post('http://127.0.0.1:8080/Webcontent/api/mail', $scope.formData).success(function(data) {
//alert("inside success");
$scope.formData = {};
$scope.users = data;
//console.log(data);
})
.error(function(data) {
//alert("Bad Luck....");
console.log('Error: ' + data);
});
};
Just change in your route.js file:
app.post('/Webcontent/api/mail', function(req, res) {
console.log('inside post');
Mail.fire(req.body.text1);
console.log(req.body.text1);
});