Skip to content

Instantly share code, notes, and snippets.

@scilganon
Last active January 8, 2020 21:05
Show Gist options
  • Save scilganon/7a8aab687989e52ada1a78b22560bdef to your computer and use it in GitHub Desktop.
Save scilganon/7a8aab687989e52ada1a78b22560bdef to your computer and use it in GitHub Desktop.
Proxy http server to transform any img to webp
const http = require('http');
const imgmin = require('imagemin-webp');
const r = require('request');
const imgPath = 'http://cdn.pixabay.com/photo/2017/05/13/23/05/img-src-x-2310895_960_720.png';
const server = http.createServer((req, res) => {
const request = r.defaults({ encoding: null });
request.get(imgPath, async (err, iRes, body) => {
try {
const img = await imgmin({ quality: 50})(body);
res.writeHead(200, {
'Content-Type': 'image/webp',
'Content-Length': img.byteLength,
});
res.write(img, 'binary');
} catch(e) {
console.error(e);
res.writeHead(500);
}
res.end();
});
});
server.listen(8080);
{
"name": "proxy_img",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"imagemin-webp": "^5.1.0",
"request": "^2.88.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment