Some research on require ("http"). Request (options, CB)

Source: Internet
Author: User

I decided to use couthdb as my default database, so I used HTTP. request, but the request could not be sent out.

Locate row 1144

// Require ("HTTP "). request (options, CB) function clientrequest (options, CB) {var self = This; outgoingmessage. call (Self); self. agent = options. agent === undefined? Globalagent: options. agent; // whether to use the agent var defaultport = options. defaultport | 80; var Port = options. port | defaultport; var host = options. hostname | options. host | 'localhost'; If (options. sethost = undefined) {var sethost = true;} self. socketpath = options. socketpath; VaR method = self. method = (options. method | 'get '). touppercase (); self. path = options. path | '/'; If (CB) {self. once ( 'Response', CB);} If (! Array. isarray (options. headers) {If (options. headers) {var keys = object. keys (options. headers); For (VAR I = 0, L = keys. length; I

No result. This is just a class. Continue.

Exports. request = function (options, CB) {If (typeof Options = 'string') {Options = URL. parse (options);} If (options. protocol & options. protocol! = 'HTTP: ') {Throw new error ('Protocol:' + options. protocol + 'not ororted. ');} return New clientrequest (options, CB) ;}; exports. get = function (options, CB) {var Req = exports. request (options, CB); req. end (); Return req ;};

It is found that the only thing HTTP. Get needs to do more than http. request is to add a req. End () and crash!

In addition, this callback really only has one parameter, that is, the second parameter of HTTP. createserver (function (req, Res) {}) is the same!

We only need to add an end to makeProgramRunning! (First install and start couthdb)

 var HTTP = require ('http'); var Options = {port: 5984, method: 'get'}; // The callback has only one parameter, namely, HTTP. createserver (function (req, Res) {}) var Req = http. request (options, function (RES) {console. log ('status: '+ Res. statuscode); console. log ('headers: '+ JSON. stringify (res. headers); Res. setencoding ('utf8'); var body = "" res. on ('data', function (chunk) {body + = chunk}); Res. once ("end", function () {var JSON = JSON. parse (body); console. log (JSON)}; req. end () req. on ('error', function (e) {console. log ('problem with request: '+ E. message) ;}); 

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.