【问题标题】:POST request 404 (Not Found) with angular.js and ExpressJS使用 angular.js 和 ExpressJS 发布请求 404(未找到)
【发布时间】:2014-07-27 02:40:35
【问题描述】:

我正在尝试使用 AngularJS 向我的 ExpressJS 服务器发送请求:

angular.module('app').controller('contactCtrl', function($scope, $http) {
    $scope.envoyer = function(nom, organisation, courriel, telephone, message){
        $http.post('/contact', {nom:nom, organisation:organisation, courriel:courriel, telephone:telephone, message:message}).then(function(error, response){
            if(response.data.success){
                console.log('sent!');
            }
        });
    }
});

这是来自服务器的代码:

var mailer = require('nodemailer');

var EMAIL_ACCOUNT_EMAIL = 'aaa@gmail.com';
var EMAIL_ACCOUNT_PASSWORD = 'aaa';

var smtpTransport = mailer.createTransport({
    service: "Gmail",
    auth: {
        user: EMAIL_ACCOUNT_EMAIL,
        pass: EMAIL_ACCOUNT_PASSWORD
    }
});

app.post('/contact', function(req, res, next){
        var success = true;
        var mailOptions = {
            to: 'azez@email.com',
            subject: 'qsdxwccvfd',
            html: 'sqdqsdq'
        };

        smtpTransport.sendMail(mailOptions, function(error, res){
            if(error){
                console.log('[ERROR] Message NOT sent: ', error);
                success = false;
            } else {
                console.log('[INFO] Message sent: ' + res.message);
            }
            next(error, success);
        });
});

服务器执行请求后,我在客户端收到此错误消息:

POST http://localhost:3002/contact 404 (Not Found) angular.js:8495
(anonymous function) angular.js:8495
sendReq angular.js:8291
$http.serverRequest angular.js:8025
wrappedCallback angular.js:11498
wrappedCallback angular.js:11498
(anonymous function) angular.js:11584
Scope.$eval angular.js:12608
Scope.$digest angular.js:12420
Scope.$apply angular.js:12712
(anonymous function) angular.js:18980
jQuery.event.dispatch jquery.js:4409
elemData.handle

我认为它来自 AngularJS 代码的第四行,但我无法找到解决方法。请问我该如何解决?

【问题讨论】:

  • 服务器提示http://localhost:3002/contact没有资源。你期待有什么东西在那里吗?什么代码提供了该端点的实现?
  • /contact 的中间件不返回响应。假设这是整个服务器代码(并且没有稍后的中间件处理它),那么对 next 的调用将返回您的 404。如果您想要一个返回 JSON 数据供客户端解析的示例,请告诉我我可以写一个,假设那是一个express 应用程序。
  • @MartinAtkins,事实上,当发送的电子邮件有效时,我试图在客户端显示一个 toastr 通知,我如何在响应中设置一个成功属性并显示一个 toastr另一边的通知?
  • @MartinAtkins,你说得对,我正在使用 expressjs。

标签: javascript node.js angularjs express


【解决方案1】:

为了向浏览器发送成功的响应,中间件末尾对next 的调用应替换为以下内容:

res.set('Content-Type', 'application/json'); // tell Angular that this is JSON
res.send(JSON.stringify({success: success}));

这些行告诉 express 返回一个标记为成功的响应,其主体为 JSON 格式,对象仅具有“成功”属性。

但是,为了使其正常工作,您需要重命名您在回调中使用的参数 smtpTransport.sendMail,因为您调用了 res,因此它目前对中间件隐藏了 res 对象。因此,将该函数的标头更改为以下内容:

smtpTransport.sendMail(mailOptions, function(error, mailRes){

...当然,您必须将后面的日志行更改为使用mailRes 而不是res

【讨论】:

  • 我收到此错误消息:TypeError: Object #<Object> has no method 'set'
  • 您是否按照我的指示更新了mailRes 参数?如果 res 仍然是邮件发送结果而不是 Express 响应对象,我预计会出现该错误。
  • 你能在res.set调用之前添加console.log(res),这样我们就可以看到这个对象到底是什么?看起来它不是 Express 响应对象。
  • 我收到了{ accepted: [ 'aaa@gmail.com' ], rejected: [], response: '250 2.0.0 OK 1406434616 ed15sm14788940wic.9 - gsmtp', envelope: { from: false, to: [ 'aaa@gmail.com' ] }, messageId: '1406434615279-ca07793a-8e5861d7-da24b177@localhost' } 的回复
  • 这看起来像是smtpTransport.sendMail 给我的回复。请确保变量名res 仅在app.post('/contact', function(req, res, next){ 行和我在回复中给出的行中使用。该变量名不应在函数的其他任何地方使用。
【解决方案2】:

我在 express 4.0 中遇到了同样的问题。我通过重新安装 body-parser 来修复它:

npm install body-parser

然后做:

git add -A

【讨论】:

    【解决方案3】:

    亲爱的,它工作得很好。您为 smtpTransport 响应指定了与 "res" 相同的名称,只需更改它即可。它会正常工作。对于 snnipet,请参考下面给出的示例。

    app.post('/contact', function (req, res, next) {
        var success = true;
        var mailOptions = {
            to: 'deepak.m.shrma@email.com',
            subject: 'hey',
            html: 'hello'
        };
        smtpTransport.sendMail(mailOptions, function (error, result) {
            if (error) {
                console.log('[ERROR] Message NOT sent: ', error);
                success = false;
                res.send(error)
            } else {
                console.log('[INFO] Message sent: ' + result);
            }
            res.send(result)
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 1970-01-01
      • 2021-09-09
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多