Last active
April 10, 2018 07:25
-
-
Save jsuryahyd/26ace97dbb0f09402d5b8e31163c678f to your computer and use it in GitHub Desktop.
Port farwarding in nodejs
This file contains 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
const http = require('http'); | |
http.createServer((req,res)=>{ | |
let route = req.url; | |
if(route == '/'){ | |
res.end('root route.') | |
}else if(route == '/hello'){ | |
res.end('hello') | |
} | |
}).listen(3000,()=>{console.log('listening on a domain')}) | |
/** | |
* @link https://gist.github.com/extronics/2043518 | |
* run command :- node server.js 80 127.0.0.1 3000 | |
* go to browser and http://localhost/ | |
*/ | |
const net = require('net'); | |
domainPort = process.argv[2]; | |
targetHost = process.argv[3]; | |
targetPort = process.argv[4]; | |
net.createServer((connIn)=>{ | |
let connOut = net.createConnection(targetPort,targetHost); | |
connIn.pipe(connOut); | |
connOut.pipe(connIn); | |
connOut.on("end",connIn.end.bind(connIn)); | |
connIn.on('end',connOut.end.bind(connOut)) | |
}).listen(domainPort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment