我正在编写一个程序,它将使用nodemailer点击html中的按钮发送邮件。 我的应用程序服务器在8383端口上运行,节点服务器在8080上运行。 我收到错误" POST
http://127.0.0.1:8080/Webcontent/api/mail
404(未找到)127.0.0.1:8080/Webcontent/api/mail
:1 错误:无法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);
});
};
答案 0 :(得分:0)
只需更改您的route.js文件:
app.post('/Webcontent/api/mail', function(req, res) {
console.log('inside post');
Mail.fire(req.body.text1);
console.log(req.body.text1);
});