Last active
April 14, 2024 08:50
-
-
Save nektro/1a3bc2f7cf8db85a3cae46efce070bdd to your computer and use it in GitHub Desktop.
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 sites = { | |
"foo.localhost": 8001, | |
"bar.localhost": 8003, | |
"qux.localhost": 8004, | |
"sam.localhost": 8005, | |
}; | |
// new version based on: | |
// https://stackoverflow.com/questions/20351637/how-to-create-a-simple-http-proxy-in-node-js | |
const http = require('http'); | |
const server = http.createServer((client_req, client_res) => { | |
console.log('serve:', client_req.method, client_req.headers.host, client_req.url); | |
console.log('serve:', "localhost", sites[client_req.headers.host], client_req.url); | |
console.log(); | |
const options = { | |
hostname: '127.0.0.1', | |
port: sites[client_req.headers.host] ?? 9000, | |
path: client_req.url, | |
method: client_req.method, | |
headers: client_req.headers | |
}; | |
const proxy = http.request(options, function (res) { | |
client_res.writeHead(res.statusCode, res.headers) | |
res.pipe(client_res, { | |
end: true | |
}); | |
}); | |
client_req.pipe(proxy, { | |
end: true | |
}); | |
}); | |
server.listen(80); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment