2015-10-05 55 views
2

我有一個角度的應用程序與表單,使ajax請求。電子郵件工作正常,但不管我設定的迴應是什麼;我得到了一條路徑錯誤。我假設節點期望路徑'/ send'來呈現模板或數據,但我只想要電子郵件的路徑!〜我使用模式彈出式窗口&角度ui路由選擇路徑。發送電子郵件時Node.js 404錯誤,忽略我的res.status

app.get('/send', function (req, res, err) { 
    var cartContent = req.body.slice(1,20); 
    var content = tableify(cartContent); 
    var sendit = { 
    to: '*****@gmail.com', 
    from: '****@bluenightphoto.com', 
    replyTo: req.body[0].email, 

    subject: "CP Carpet Quote Request: " + req.body[0].fname + " " + req.body[0].lname , // REQUIRED. 

    html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " + 
    '<br>' + 'Message: ' + req.body[0].message + '<br>' + <b>Table</b>'+ '<br><br>' + content, 
    }; 

    // Transporter refers to nodemailer and sendit is the login details(mail works fine!) 

    transporter.sendMail(sendit, function (req, res, err) { 
    if (err) { 
     console.log(err + "something strange..."); 
     res.status(401).send("that's all folk's"); 
    } else { 
     res.status(200).send("nuthing here"); 
     console.log("Message sent! "); 
    } 

    transporter.close(); 
    }); 
}); 

然而,即使電子郵件成功,我的錯誤處理程序總是得到一個404響應。

編輯:試了下面的兩個解決方案,沒有工作。

這裏是角Ajax代碼

var makeForm = function() { 
       if (mailJson[1].total !== 0) { 
        deferred.resolve(mailJson); 
        console.log('values loaded successfully again'); 


        $http({ 
         method : 'GET', 
         url : '/send', 
         data : mailJson, 
         headers : { 
          'Content-type' : 'application/json' 
         } 
        }).success(function() { 
         console.log('success!'); 
         $scope.alertEmailOk = true; 
        }).error(function (err) { 
         console.log('there was an error indeed ' + err); 
         $scope.alertEmailOk = false; 
        }); 
       } else { 
        console.log('no values!'); 
        deferred.reject(mailJson); 
       } 
       return deferred.promise; 
      }; 

的console.log( '有一個錯誤確實' + ERR);總是火災..可能需要發回數據?

這裏是我的快件代碼中的錯誤處理程序:

if (app.get('env') === 'production') { 

    // changes it to use the optimized version for production 
    app.use('/', express.static(path.join(__dirname, '/dist'))); 

    // production error handler 
    // no stacktraces leaked to user 
     app.use(function(err, req, res, next) { 
     res.status(err.status || 500); 
     res.render('error', { 
      message: err.message, 
      error: err 
     }); 
    }); 
} 
+0

通過異步獲得了一些信息,但我確信我們在這裏丟失了一小段代碼。該路由控制器中的其他任何東西?另外,中間件是'(req,res,next)' –

+0

它與'angularjs'有關嗎?如果它超出了等式 - 然後刪除標籤以避免飢餓的'angularjs'大腦。 – ilyaigpetrov

+0

我添加了角度ajax代碼。 – deek

回答

0
transporter.sendMail(sendit,function(error, info){ 
if (error) { 
    console.log(error + "something strange..."); 
    } 

console.log("Message sent!"); 
}); 

    res.send("Messaage was Sent"); 

return next(); 
}); 

這工作。嘗試在傳輸函數中設置res.send對象導致它不會觸發。把它放在外面讓它很好,現在我沒有任何錯誤。

+0

現在你有問題了嗎?或者一切都像你想要的那樣工作? – Anonymous0day

+0

沒問題,它解決了100%..我很驚訝我可以讓res.send在運輸車內工作。 – deek

0

這裏有一些修正和評論:

app.get('/send', function (req, res, next) { 
    // ... 
    transporter.sendMail(sendit, function (req, res, next) { // Asynchronous = do it later. 
    // Parent arg `res` is masked by another `res` which is passed by `sendMail`. 
    // I don't know if `sendMail` passes the same one or some other `res`. 
    // You may try renaming it to `res2`. 
    } 
    return next(); // For now call next middleware, all async code will be executed later. 
+0

試過,沒有工作 – deek

1
// Transporter refers to nodemailer and sendit is the login details(mail works fine!) 

我看了你的評論,但首先我們可以肯定的下一部分: 從nodemailer

var nodemailer = require('nodemailer'); 
var transporter = nodemailer.createTransport({ 
    service: 'gmail', 
    auth: { 
     user: '[email protected]', 
     pass: 'password' 
    } 
}); 
var sendIt = { //<------ i've just renamed this compare to nodemailer.com sample 
    from: '[email protected]', 
    to: '[email protected]', 
    subject: 'hello', 
    text: 'hello world!' 
}; 


// this part is very important ! 
// note the signature. 
transporter.sendMail(sendIt, function(error, info){ 
    if(error){ 
     return console.log(error); 
    } 
    console.log('Message sent: ' + info.response); 

}); 

所以現在嘗試插入您的代碼:

//app.get('/send', function (req, res, err) { 
app.get('/send', function (req, res, next) { 

    var cartContent = req.body.slice(1,20); 
    var content = tableify(cartContent); 
    var sendit = { 
    to: '*****@gmail.com', 
    from: '****@bluenightphoto.com', 
    replyTo: req.body[0].email, 

    subject: "CP Carpet Quote Request: " + req.body[0].fname + " " + req.body[0].lname , // REQUIRED. 

    html: '<b>Customer Quote</b>' + '<br>' + 'Customer Name: ' + req.body[0].fname + " " + 
    '<br>' + 'Message: ' + req.body[0].message + '<br>' + <b>Table</b>'+ '<br><br>' + content, 
    }; 

    // Transporter refers to nodemailer and sendit is the login details(mail works fine!) 
    //  |     
    //  | 
    //  v 
    // Transporter refers to the login details and sendit to the mail options 


    transporter.sendMail(sendit, function (error, info) {// instead of (req, res, err) { 
    if (error) { // instead of err just to avoid colision name 
     console.log(error + "something strange..."); 
     res.status(401).send("that's all folk's"); 
    } else { 
     res.status(200).send("nuthing here"); 
     console.log("Message sent! "); 
    } 

    transporter.close(); 
    }); 

return next(); 
}); 
在你的代碼

,您的問題在這裏:

transporter.sendMail( sendit, function ( req, res, err) { 
//transporter.sendMail(sendit, function (error, info) { 

    if (err) { 
     console.log(err + "something strange..."); 
     res.status(401).send("that's all folk's"); 

     // you are acting here on the 'info' coming from transporter.sendMail 
    // not on 'res' from app.get('/send',  


    } else { 
     res.status(200).send("nuthing here"); 
     console.log("Message sent! "); 


     // you are acting here on the 'info' coming from transporter.sendMail 
    // not on 'res' from app.get('/send',  

    } 

試試你的問題解釋上述這種變化,我希望這將解決你的問題。 :-)

+0

沒有工作,我添加了我的錯誤處理程序和ajax角碼到原來的問題。 – deek

+0

如果你用'return next();'代替'return next();'會發生什麼?' – Anonymous0day

+0

我給了你賞金,你的回答讓我朝着正確的方向 – deek