Skip to content

Instantly share code, notes, and snippets.

@IAmRC1
Created May 25, 2020 10:40
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 IAmRC1/68fc64536d53fe8e421f61aca82df505 to your computer and use it in GitHub Desktop.
Save IAmRC1/68fc64536d53fe8e421f61aca82df505 to your computer and use it in GitHub Desktop.
Proxy for php
/**
* Failed to minify the file using UglifyJS v3.4.3. Serving the original version.
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
console.log('hello')
const express = require('express');
const url = require('url');
const cors = require('cors');
const request = require('request');
function validUrl(req, res, next) {
if (!req.query.url) {
next(new Error('No url specified'));
} else if (typeof req.query.url !== 'string' || url.parse(req.query.url).host === null) {
next(new Error(`Invalid url specified: ${req.query.url}`));
} else {
next();
}
}
module.exports = () => {
const app = express.Router();
app.get('/', cors(), validUrl, (req, res, next) => {
switch (req.query.responseType) {
case 'blob':
req.pipe(request(req.query.url).on('error', next)).pipe(res);
break;
case 'text':
default:
request({url: req.query.url, encoding: 'binary'}, (error, response, body) => {
if (error) {
return next(error);
}
res.send(
`data:${response.headers['content-type']};base64,${Buffer.from(
body,
'binary'
).toString('base64')}`
);
});
}
});
return app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment