Error:can ' t set headers after they is sent.

Source: Internet
Author: User
Tags response code windows 7 x64

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.