Skip to content

Instantly share code, notes, and snippets.

@zdying
Created June 25, 2017 07:00
Show Gist options
  • Save zdying/98dff80efeb072e55e7318fe6670905f to your computer and use it in GitHub Desktop.
Save zdying/98dff80efeb072e55e7318fe6670905f to your computer and use it in GitHub Desktop.
proxy Node.js http/https request without any library
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