【问题标题】:Node.js and Sendgrid mailer error on res.endres.end 上的 Node.js 和 Sendgrid 邮件程序错误
【发布时间】:2013-02-13 22:56:26
【问题描述】:

我对节点有点陌生。我正在使用 express 和 sendgrid api 发送电子邮件(完全收集 REST)。 sendgrid 成功或失败后,我想用一个 json 对象来响应。这是示例案例:

var SendGrid = require('sendgrid-nodejs').SendGrid;
var sendgrid = new SendGrid(user, key);

app.get('/LGP/:email', function (req, res){
    sendgrid.send({
        to: req.params.email,
        from: 'me@example.com',
        subject: 'Hello World',
        text: 'This email sent through SendGrid'
        }, function(success, message) {
            if (!success) {
                console.log(message);
            } else {
                res.writeHead(200, { 'Content-Type': 'application/json' });
                res.write(JSON.stringify({ result: 'success' }));
                res.end(); //error occurs: "Can't use mutable header APIs after sent."
            }
        }
    );
});

在我的本地服务器上(使用工头),一切正常。但是当我把它推送到heroku时,它给了我这个堆栈跟踪:

2013-02-27T22:12:46+00:00 app[web.1]: http.js:543
2013-02-27T22:12:46+00:00 app[web.1]:     throw new Error("Can't use mutable header APIs after sent.");
2013-02-27T22:12:46+00:00 app[web.1]:           ^
2013-02-27T22:12:46+00:00 app[web.1]: Error: Can't use mutable header APIs after sent.
2013-02-27T22:12:46+00:00 app[web.1]:     at ServerResponse.getHeader (http.js:543:11)
2013-02-27T22:12:46+00:00 app[web.1]:     at /app/node_modules/express/node_modules/connect/lib/middleware/logger.js:229:26
2013-02-27T22:12:46+00:00 app[web.1]:     at ServerResponse.<anonymous> (/app/node_modules/express/node_modules/connect/lib/middleware/logger.js:149:20)
2013-02-27T22:12:46+00:00 app[web.1]:     at /app/app.js:60:13
2013-02-27T22:12:46+00:00 app[web.1]:     at IncomingMessage.<anonymous> (/app/node_modules/sendgrid/lib/sendgrid.js:74:9)
2013-02-27T22:12:46+00:00 app[web.1]:     at IncomingMessage.emit (events.js:81:20)
2013-02-27T22:12:46+00:00 app[web.1]:     at HTTPParser.onMessageComplete (http.js:133:23)
2013-02-27T22:12:46+00:00 app[web.1]:     at CleartextStream.ondata (http.js:1213:22)
2013-02-27T22:12:46+00:00 app[web.1]:     at CleartextStream._push (tls.js:291:27)
2013-02-27T22:12:46+00:00 app[web.1]:     at SecurePair._cycle (tls.js:565:20)
2013-02-27T22:12:48+00:00 heroku[web.1]: Process exited with status 1
2013-02-27T22:12:48+00:00 heroku[web.1]: State changed from up to crashed

/app/app.js:60:13 指的是带有“res.end()”的行。我做错了什么?

【问题讨论】:

  • 这可能是因为 res.end() 被多次调用。您应该添加一些日志来检查 sendgrid 的完成功能是否多次调用您。您还应该将var SendGrid = require(...) 移出app.get。第一个调用是同步加载模块。
  • 在 writeHead 之前将 require 更改为外部并添加了一个 console.log(),但我只看到日志一次,它仍然在 req.end() 上崩溃。奇怪的是它只发生在服务器上而不是本地时。
  • node.js和sendgrid版本一样吗?
  • 嗯,不知道有什么不同?据我了解,我不只是在 package.json 中设置所有内容吗?当我提交时,它也会被推送到服务器。
  • 对于模块是的,但是您是否指定“*”,或者为 express、sendgrid 指定确切的版本?节点版本也可能很重要。最近对 node.js http 支持的许多小变化。

标签: node.js heroku http-headers express sendgrid


【解决方案1】:

为了最大限度地提高您的应用在本地和 heroku 上表现相同的机会,您应该确保为所有模块指定特定版本,并避免将 "*" 用于主要的 dependencies

还应该在 package.json 中指定节点版本:

"engines": {
  "node": "0.8.20"
}

(使用任何合适的版本)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多