JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

12.2.2 Node Example: HTTP Client Utilities Module

12.2.2 Node Example: HTTP Client Utilities Module


Example 12-3 uses the “http” module to define utility functions for issuing HTTP GET and POST requests. The example is structured as an “httputils” module, which you might use in your own code like this:

var httputils = require("./httputils"); // Note no ".js" suffix httputils.get(url, function(status, headers, body) { console.log(body); });

The require() function does not execute module code with an ordinary eval(). Modules are evaluated in a special environment so that they cannot define any global variables or otherwise alter the global namespace. This special module evaluation environment always includes a global object named exports. Modules export their API by defining properties in this object.2

Example 12-3. Node “httputils” module

// // An "httputils" module for Node. //

// Make an asynchronous HTTP GET request for the specified URL and pass the // HTTP status, headers and response body to the specified callback function. // Notice how we export this method through the exports object. exports.get = function(url, callback) {

2. Node implements the CommonJS module contract, which you can read about at http://www.commonjs .org/specs/modules/1.0/ .

// Parse the URL and get the pieces we need from it url = require('url').parse(url); var hostname = url.hostname, port = url.port || 80; var path = url.pathname, query = url.query; if (query) path += "?" + query;

// Make a simple GET request var client = require("http").createClient(port, hostname); var request = client.request("GET", path, {

"Host": hostname // Request headers }); request.end();

// A function to handle the response when it starts to arrive

request.on("response", function(response) { // Set an encoding so the body is returned as text, not bytes response.setEncoding("utf8"); // Save the response body as it arrives var body = "" response.on("data", function(chunk) { body += chunk; }); // When response is complete, call the callback response.on("end", function() {

if (callback) callback(response.statusCode, response.headers, body); }); }); };

// Simple HTTP POST request with data as the request body

exports.post = function(url, data, callback) { // Parse the URL and get the pieces we need from it url = require('url').parse(url); var hostname = url.hostname, port = url.port || 80; var path = url.pathname, query = url.query; if (query) path += "?" + query;

// Figure out the type of data we're sending as the request body var type; if (data == null) data = ""; if (data instanceof Buffer) // Binary data

type = "application/octet-stream"; else if (typeof data === "string") // String data type = "text/plain; charset=UTF-8";

else if (typeof data === "object") { // Name=value pairs data = require("querystring").stringify(data); type = "application/x-www-form-urlencoded";

}

// Make a POST request, including a request body var client = require("http").createClient(port, hostname); var request = client.request("POST", path, {

"Host": hostname,

"Content-Type": type }); request.write(data); // Send request body request.end(); request.on("response", function(response) { // Handle the response

12.2 Asynchronous I/O with Node | 303

response.setEncoding("utf8"); // Assume it is text var body = "" // To save the response body response.on("data", function(chunk) { body += chunk; }); response.on("end", function() { // When done, call the callback

if (callback) callback(response.statusCode, response.headers, body); }); }); };

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31406.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1