Sometimes you need a proxy.
- Get any from google
- Write your own. That's pretty easy:
const http = require('http');
http.createServer(handler).listen(3000);
function handler(req, res) {
console.log('serve: ' + req.url);
const options = {
hostname: 'example.com',
port: 80,
path: req.url,
method: req.method
};
const proxy = http.request(options, _res => {
_res.pipe(res, {
end: true
});
});
req.pipe(proxy, {
end: true
});
}
- Try out this weird thing just for fun.
- Unix compliant OS
- NodeJS 8+
npm i qorsproxy --save-dev
Any of you'd prefer:
npm start -- --config=path
pm2 start npm --name qorsproxy -- start -- --port=8080 --config=/Users/a.golub/repo/qorsproxy/config/qorsproxy.dev.qiwi.tools.json
npm run start_pm2 -- -- --port=8080
--config
sets path to custom config--port
defines server listen port. Defaults to9292
--host
DNS name or IP address
{
"server": {
"host": "127.0.0.1",
"port": 8080
},
"rules": {
"localhost": {
"from": [
"*"
],
"to": [
"example.com"
],
"mutations": [
{
"direction": "to",
"headers": [
{
"name": "origin",
"value": "localhost"
}
]
},
{
"direction": "from",
"headers": [
{
"name": "set-cookie",
"value": {
"from": "/;Domain.+;/",
"to": ";Domain: foobar.com;"
}
}
]
}
]
}
}
}
If you need support for OPTIONS request, extend target rule:
"interceptions": [
{
"req": {
"method": "OPTIONS"
},
"res": {
"status": 200
}
}
],
curl 'http://127.0.0.1:9292/http://example.com' -H 'origin:http://localhost' → <!doctype html> ...