How to proxy HTTP request with NodeJS
For my data compression Chrome extension I needed a proxy server,
which should download the original file, compress it and respond back to browser.
It was pretty straight forward to implement using NodeJS and Express.
To handle the whole process of conversion in memory without touching the file system, NodeJs Streams come in handy. I used request library to handle image download and sharp library to convert downloaded image. Both of them support stream interface and work together quite nicely:
I used transform stream provided by
sharp, but you can easily implement your own custom transformer
to perform any kind of changes to the response from original server before returning it.
Here’s the full implementation of my compression proxy.