Skip to content

Instantly share code, notes, and snippets.

@abovethewater
Last active April 15, 2020 14:32
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 abovethewater/0e8cf2b35c9eea6a006d7408eb8a656d to your computer and use it in GitHub Desktop.
Save abovethewater/0e8cf2b35c9eea6a006d7408eb8a656d to your computer and use it in GitHub Desktop.
Quick proxy to illustrate the use of the content-type to fix extra characters in non utf-8 encoding of Copyright ©
const http = require('http')
const httpProxy = require('http-proxy')
const options = {
target: 'https://free.co.uk'
}
const proxy = httpProxy.createProxyServer({
secure: false,
changeOrigin: true,
target: 'http://www.cloudflare.com'
})
proxy.on('proxyRes', function (proxyRes, req, res) {
// A very blunt approach
// set the encoding to utf-8 to avoid the extra character
// Copyright © 2019-2020 with content-type: text/html
// Copyright © 2019-2020 with content-type text/html; charset=utf-8
if (proxyRes.headers['content-type'] === 'text/html') {
proxyRes.headers['content-type'] = 'text/html; charset=utf-8'
}
})
const requestListener = function (req, res) {
proxy.web(req, res, options)
}
const server = http.createServer(requestListener)
server.on('error', err => {
console.log('Server error: \n', err)
})
proxy.on('error', (err, req, res) => {
console.log('Proxy server error: \n', err)
res.writeHead(500, JSON.stringify(err.message))
res.end()
})
server.listen(3000, () => {
console.log('listening on port ' + 3000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment