Creating Web Client Exceptions with Node

  node.js, question

Create a Web client using Node

The server is already open

clipboard.png

Js

var http = require('http');
 
 
 
 //Options for Requests
 var options = {
 host: 'http://127.0.0.1',
 port: '8080',
 path: 'index.html'
 };
 
 //callback function to process response
 var callback = function(response){
 //Keep updating data
 var body = '';
 response.on('data', function(data) {
 Body plus = data;
 });
 
 //Data reception completed
 response.on('end', function() {
 console.log(body);
 });
 bracket
 
 //Send a request to the server
 var req = http.request(options, callback);
 
 //End request
 req.end();

Running the js file reported an error.

clipboard.png

LessrequestTheerror handler, change it:

var http = require('http');
 
 
 
 //Options for Requests
 var options = {
 protocol: 'http:',
 host: '127.0.0.1',
 port: '8080',
 path: '/index.html'
 };
 
 //callback function to process response
 var callback = function(response){
 //Keep updating data
 var body = '';
 response.on('data', function(data) {
 Body plus = data;
 });
 
 //Data reception completed
 response.on('end', function() {
 console.log(body);
 });
 bracket
 
 //Send a request to the server
 var req = http.request(options, callback);
 
 //Error in processing request
 req.on('error', function(e) {
 Log ('problemwithrequest:' plus e');
 });
 
 //End request
 req.end();

Look at documents:

图片描述

Address:http