Created
June 25, 2017 07:00
-
-
Save zdying/98dff80efeb072e55e7318fe6670905f to your computer and use it in GitHub Desktop.
proxy Node.js http/https request without any library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var options = { | |
// proxy server ip | |
hostname: '127.0.0.1', | |
// proxy server port | |
port: 5525, | |
// the request url (absolute path) | |
path: 'http://www.example.com/', | |
method: 'GET', | |
// should set Host header. or it will be `127.0.0.1:5525` | |
headers: { | |
'Host': 'www.example.com' | |
} | |
}; | |
var req = http.request(options, function (res) { | |
res.on('data', function (data) { | |
console.log('data arrive ==>', data.toString()); | |
}); | |
}); | |
req.end(); | |
// var https = require('https'); | |
// var options = { | |
// hostname: '127.0.0.1', | |
// port: 10010, | |
// path: 'https://nodejs.org/en/', | |
// method: 'GET', | |
// headers: { | |
// 'Host': 'nodejs.org' | |
// }, | |
// rejectUnauthorized: false | |
// }; | |
// var req = https.request(options, function (res) { | |
// res.on('data', function (data) { | |
// console.log('data arrive ==>', data.toString()); | |
// }); | |
// }); | |
// req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment