Node.js和Sendgrid邮件程序错误

我对节点有点新鲜 我使用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()”的行。 我究竟做错了什么?

       

网上收集的解决方案 "Node.js和Sendgrid邮件程序错误"