Skip to content

Instantly share code, notes, and snippets.

@nleush
Last active February 22, 2022 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nleush/7916ee89f7b8d6f0cd478d7335702139 to your computer and use it in GitHub Desktop.
Save nleush/7916ee89f7b8d6f0cd478d7335702139 to your computer and use it in GitHub Desktop.
Here’s the sample proxy/echo service for `Iframely` to use with `@adobe/helix-fetch`. Put `CONFIG.PROXY_URL="http://<adress>:8089/proxy?url="` in your `Iframely` config to use this proxy.
const moment = require('moment');
module.exports = function log() {
var args = Array.prototype.slice.apply(arguments);
args = args.filter(i => typeof i !== 'undefined' && i !== null);
args.splice(0, 0, "--", "[" + moment().utc().format("YY-MM-DD HH:mm:ss") + "]");
console.log.apply(console, args);
};
{
"dependencies": {
"@adobe/helix-fetch": "^3.0.0",
"express": "^4.17.1",
"moment": "^2.29.1"
}
}
const { noCache } = require('@adobe/helix-fetch');
const express = require('express');
const app = express();
const log = require('./logging');
const port = 8089;
const fetch = noCache({
h1: {
keepAlive: true
},
rejectUnauthorized: false // By default skip auth check for all.
}).fetch;
app.get('/proxy', function(req, res) {
var url = req.query.url;
if (!url) {
res.status(400).send('"url" param required');
return;
}
log('GET /proxy', url);
const timer = new Date().getTime();
const headers = Object.assign({}, req.headers);
delete headers.connection; // `connection` header is forbidden for `fetch`.
delete headers.host; // `host` is wrong to proxy.
fetch(url, {
headers: headers,
redirect: 'manual',
})
.then(response => {
const location = response.headers.get('location');
var location_str;
if (location) {
location_str = 'redirect to: ' + location;
}
const duration = new Date().getTime() - timer;
log(' response (in ' + duration + 'ms):', response.status, location_str, 'for:', url);
res.status(response.status);
const headers = response.headers.plain();
delete headers['content-encoding']; // Remove 'gzip' and other encoding.
delete headers['content-length']; // Remove incorrect size for ungzipped.
res.set(headers);
response.body.pipe(res);
})
.catch(error => {
log(' error:', error.message, 'for:', url);
res.status(400).send('Error making request: ' + error.message);
});
});
process.on('uncaughtException', function(err) {
log(err.stack);
});
process.title = "proxy";
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment