I am requesting an index.djvu that fires requests for documents by id. The requests id that the index.djvu fires is Base64 encoded e.g document/page_1_1_1_k21g_MjE4MGstMTAvMTE=.djvu
where the id is MjE4MGstMTAvMTE=
.
I need to encode the id before the requests is sent e.g encodeURIComponent('MjE4MGstMTAvMTE=')
.
How can I get control over the requests that the index.djvu fires?
My code looks like this
var http = require('http');
var httpProxy = require('http-proxy');
var HttpProxyRules = require('http-proxy-rules');
var proxyRules = new HttpProxyRules({
rules: {
// This requests response index further requests explained
'.*/document': 'https://api.service.xxx/document/index.djvu?archive=21&id=2180k-10/11'
},
default: 'https://api.service.xxx/document'
});
var proxy = httpProxy.createProxyServer({
secure: false
});
proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.setHeader('Authorization', 'Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
});
var server = {};
server.httpServer = http.createServer(function (req, res) {
var target = proxyRules.match(req);
if (target) {
return proxy.web(req, res, {
target: target
});
}
});
server.init = () => {
server.httpServer.listen(9000, () => {
console.log('\x1b[36m%s\x1b[0m', 'The HTTP server is running on port 9000');
});
}
server.init();
server.httpServer.on('error', function (err) {
console.log('Error http-server\n', JSON.stringify(err));
});
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('Status: ' + proxyRes.statusCode + ' ' + proxyRes.statusMessage);
proxyRes.on('data', function (chunk) {
console.log('Body: ' + chunk.toString('utf8'));
});
console.log('Headers received from api: ', JSON.stringify(proxyRes.headers, true, 2));
});
All help is appreciated