Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created February 16, 2017 21:50
Show Gist options
  • Save mooyoul/e4e2571c4b3b147150ea5628a7018367 to your computer and use it in GitHub Desktop.
Save mooyoul/e4e2571c4b3b147150ea5628a7018367 to your computer and use it in GitHub Desktop.
disposed request stream data
'use strict';
const http = require('http');
const url = require('url');
const request = require('request');
const server = http.createServer((req, res) => {
const destUrl = req.url.slice(1);
if (!destUrl) {
res.writeHead(400);
return res.end('Please supply destination url');
}
const parsedDest = url.parse(destUrl);
if (!(parsedDest && parsedDest.protocol)) {
res.writeHead(400);
return res.end('Unsupported or missing protocol');
}
request({
method: 'GET',
url: destUrl,
encoding: null
}).on('response', (_res) => {
if (_res.statusCode.toString().slice(0, 1) !== '2') {
res.writeHead(502);
return res.end();
}
setTimeout(() => {
_res.pipe(res);
}, 100);
});
});
server.listen(9000, () => {
console.log('Server listening on port 9000...');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment