| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 05b81f3 commit 4099d1e
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -446,8 +446,10 @@ followed by `response.end()`. | |||
| 446 | 446 | ## http.request(options, callback) | |
| 447 | 447 | ||
| 448 | 448 | Node maintains several connections per server to make HTTP requests. | |
| 449 | - This function allows one to transparently issue requests. `options` align | ||
| 450 | - with [url.parse()](url.html#url.parse). | ||
| 449 | + This function allows one to transparently issue requests. | ||
| 450 | + | ||
| 451 | + `options` can be an object or a string. If `options` is a string, it is | ||
| 452 | + automatically parsed with [url.parse()](url.html#url.parse). | ||
| 451 | 453 | ||
| 452 | 454 | Options: | |
| 453 | 455 | ||
@@ -528,18 +530,12 @@ There are a few special headers that should be noted. | |||
| 528 | 530 | ## http.get(options, callback) | |
| 529 | 531 | ||
| 530 | 532 | Since most requests are GET requests without bodies, Node provides this | |
| 531 | - convenience method. The only difference between this method and `http.request()` is | ||
| 532 | - that it sets the method to GET and calls `req.end()` automatically. | ||
| 533 | + convenience method. The only difference between this method and `http.request()` | ||
| 534 | + is that it sets the method to GET and calls `req.end()` automatically. | ||
| 533 | 535 | ||
| 534 | 536 | Example: | |
| 535 | 537 | ||
| 536 | - var options = { | ||
| 537 | - host: 'www.google.com', | ||
| 538 | - port: 80, | ||
| 539 | - path: '/index.html' | ||
| 540 | - }; | ||
| 541 | - | ||
| 542 | - http.get(options, function(res) { | ||
| 538 | + http.get("http://www.google.com/index.html", function(res) { | ||
| 543 | 539 | console.log("Got response: " + res.statusCode); | |
| 544 | 540 | }).on('error', function(e) { | |
| 545 | 541 | console.log("Got error: " + e.message); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | var util = require('util'); | |
| 23 | 23 | var net = require('net'); | |
| 24 | 24 | var stream = require('stream'); | |
| 25 | + var url = require('url'); | ||
| 25 | 26 | var EventEmitter = require('events').EventEmitter; | |
| 26 | 27 | var FreeList = require('freelist').FreeList; | |
| 27 | 28 | var HTTPParser = process.binding('http_parser').HTTPParser; | |
@@ -1571,6 +1572,10 @@ ClientRequest.prototype.clearTimeout = function(cb) { | |||
| 1571 | 1572 | }; | |
| 1572 | 1573 | ||
| 1573 | 1574 | exports.request = function(options, cb) { | |
| 1575 | + if (typeof options === 'string') { | ||
| 1576 | + options = url.parse(options); | ||
| 1577 | + } | ||
| 1578 | + | ||
| 1574 | 1579 | if (options.protocol && options.protocol !== 'http:') { | |
| 1575 | 1580 | throw new Error('Protocol:' + options.protocol + ' not supported.'); | |
| 1576 | 1581 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,44 @@ | |||
| 1 | + // Copyright Joyent, Inc. and other Node contributors. | ||
| 2 | + // | ||
| 3 | + // Permission is hereby granted, free of charge, to any person obtaining a | ||
| 4 | + // copy of this software and associated documentation files (the | ||
| 5 | + // "Software"), to deal in the Software without restriction, including | ||
| 6 | + // without limitation the rights to use, copy, modify, merge, publish, | ||
| 7 | + // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
| 8 | + // persons to whom the Software is furnished to do so, subject to the | ||
| 9 | + // following conditions: | ||
| 10 | + // | ||
| 11 | + // The above copyright notice and this permission notice shall be included | ||
| 12 | + // in all copies or substantial portions of the Software. | ||
| 13 | + // | ||
| 14 | + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| 15 | + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 16 | + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
| 17 | + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| 18 | + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| 19 | + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
| 20 | + // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 21 | + | ||
| 22 | + var common = require('../common'); | ||
| 23 | + var assert = require('assert'); | ||
| 24 | + var http = require('http'); | ||
| 25 | + | ||
| 26 | + var seen_req = false; | ||
| 27 | + | ||
| 28 | + var server = http.createServer(function(req, res) { | ||
| 29 | + assert.equal('GET', req.method); | ||
| 30 | + assert.equal('/foo?bar', req.url); | ||
| 31 | + res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
| 32 | + res.write('hello\n'); | ||
| 33 | + res.end(); | ||
| 34 | + server.close(); | ||
| 35 | + seen_req = true; | ||
| 36 | + }); | ||
| 37 | + | ||
| 38 | + server.listen(common.PORT, function() { | ||
| 39 | + http.get('http://127.0.0.1:' + common.PORT + '/foo?bar'); | ||
| 40 | + }); | ||
| 41 | + | ||
| 42 | + process.on('exit', function() { | ||
| 43 | + assert(seen_req); | ||
| 44 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments