Problem Description:
System: Windows 7 x64
node. JS Version: Version is: v4.2.4 LTS
Installation process reference: "Getting Started with node. js" in Windows 7 node. JS Web Development Environment Build notes
The code for a backend impersonation HTTP request appears with the following error: Error:can ' t set headers after they is sent.
Such as:
Related code:
App.get ('/trans ', function (req, res) {var body = ';//This is the data to be submitted params = {' source ': ' en ', ' target ': ' es ', ' text ': ' Hello '}v AR data = qs.stringify (params); Http.get ("Https://gateway.watsonplatform.net/language-translation/api/v2/translate ? "+ data, function (Gres) {console.log (" Got response: "+ gres); Gres.on (' Data ', function (d) {body + = D;}). On (' End ', function () { //console.log (gres.headers); Console.log (body); Res.send (body); });}). On (' Error ', function (e) {console.log ("Got error:" + e.message);}); Res.send (body);});
cause of the problem:
The cause of this error occurs because
Http.get
The response code in is asynchronous,
But before it responds, it is the bottom sentence, "res.send (body);" The response flow has been closed.
Workaround:
will be the bottom of the sentence "res.send (body);" The comment is OK.
Error:can ' t set headers after they is sent.