HTTP Post数据对象在后端接收时未完成

时间:2015-12-04 14:03:02

标签: angularjs node.js nodemailer

我正在尝试使用AngularJS中的联系表单发送电子邮件,服务器端在NodeJS中编程,通过smtp发送电子邮件我正在使用nodemailer库,AngularJS端表单正在发送数据,但是服务器端没有收到这些数据,只显示一个名为IncomingMessage的对象有很多项,但是我看不到我的电子邮件数据。

角度方

angular.module('contactApp', [])
.factory('postEmailForm',['$http',function($http){

return {

 postEmail: function(emailData,callback){
    console.log(emailData);
   $http.post("/contact-form", emailData).success(callback);
     }
   }
}])

.controller('ContactController', function ($scope,postEmailForm) {

$scope.sendMail = function () {
console.log("Entro!");
var req = {
 headers: {
   'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-     Type, Accept'
 },
 data:
  {
    contactName : this.contactName,
    contactEmail : this.contactEmail,
    contactMsg : this.contactMsg
  }
}

  postEmailForm.postEmail(req, function (res) {
      console.log(req);
    if (res.type == false) {
      //do something
    }else{

      console.log("OK");
    }

  })

 };

});

服务器端

var express=require('express');
var nodemailer = require("nodemailer");
var app = express();
var serveStatic = require('serve-static');
var http = require('http');
app.use(serveStatic("."));
app.get('/',function(req,res){
res.sendfile('index.html');
 }); 

  var smtpTransport = nodemailer.createTransport("SMTP",{
   service: "Gmail",
  auth: {
  user: "sending email",
  pass: "sending pass"
  }
  });

 app.post('/contact-form',function(req,res){
 //Your NodeMailer logic comes here
 console.log(req.data);

 var mailOptions={
 from : req.data.contactEmail,
 to: recipient email
 subject : "Contact",
 text : req.data.contactMsg
 }
  console.log(mailOptions);
  smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
  console.log('Server error ' + error);
  res.end("error");
 }else{
 console.log("Message sent: " + response.message);
  res.end("sent");
  }
});

1 个答案:

答案 0 :(得分:0)

我忘了在服务器端添加body-parser来解析数据

var bodyParser = require('body-parser');
app.use(bodyParser.json());