node. JS Building Proxy Server request data

Source: Internet
Author: User
Tags node server

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

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.