1. Introducing modules in node. js
1 var http = require ("http"); 2 var url = require ("url"); 3 var qs = require ("querystring");
2. Create a server
//create a server with HTTP in node and pass in two parametersHttp.createserver (function(req, res) {//set the request header to allow all domain name access to resolve cross-domainRes.setheader ("Access-control-allow-origin", "*");//get the parameters in the addressvarquery =Url.parse (req.url). query;//using QS module to transform the parameters of the address into objects for easy accessvarQueryobj =qs.parse (query);//get the content from the front end of the myurl= back to the incoming datavarMyurl =Queryobj.myurl;//Create a variable to save the requested datavardata = "";//Start request Data Http.get () methodHttp.get (Myurl,function(Request) {//Listen for Myurl address request process//set the encoding formatRequest.setencoding ("UTF8");//Data signal is constantly triggered during transmissionRequest.on ("Data",function(response) {Data+=response; });//when data transfer ends trigger endRequest.on ("End",function () {//return data to the front endres.end (data); });}). On ("Error",function() {Console.log ("Request Myurl address Error!" ");});}). Listen (8989,function(err) {if(!err) {Console.log ("Server started successfully, listening 8989 ..."); } });
3. Start the server
Open the configured Nodejs folder, shift+ Open the CMD terminal in the right space
Enter the node server name. js to open the server
node. JS Building Proxy Server request data