First time working with NodeJS (yes, it’s awesome) and also using Express as well. Got the web app / service working great but I run in to problems when trying to make more than one http request. Here’s a video of how the app causes 2 http requests – http://screencast.com/t/yFKdIajs0XD – as you can see I click on ‘articles’ it loads an rss feed, then click videos and it loads a youtube feed – both work just fine but after the second call is made it throws an exception. I get the following when I attempt two separate http requests using node’s http module:
http.js:527
throw new Error("Can't set headers after they are sent.");
^
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/Users/rickblalock/node/auti_node/node_modules/express/node_modules/connect/lib/patch.js:47:22)
at /Users/rickblalock/node/auti_node/node_modules/express/node_modules/connect/lib/middleware/errorHandler.js:72:19
at [object Object].<anonymous> (fs.js:107:5)
at [object Object].emit (events.js:61:17)
at afterRead (fs.js:878:12)
at wrapper (fs.js:245:17)
Sample code provided here:
Using my controller module to render the request – http://pastie.org/2317698
One of the tabs (article tab) – the video code is identical minus referencing a video feed: http://pastie.org/2317731
try using the “end” event not “data” like this:
old answer
i also think that this is the culprit:
the first line is async so everything inside the callback is called after you do
req.write()andreq.end()put these two lines into the callback.