#nodejs #AnyProxy #ModifyJSONResponses
In this video let us see how to use AnyProxy to intercept & modify JSON responses in Node JS applications. AnyProxy is A fully configurable HTTP/HTTPS proxy in NodeJS.
Sometimes we may have the challenge to test the front end functionalities due to back end dependencies, the back end service may not be ready during the initial phase of the project.
With this, we can write rules in JavaScript to intercept specific network requests and proxy the response. Let's see now how to configure anyproxy to proxy JSON data so that the front end functionalities can be tested without waiting for the back end services.
Execute npm i -g anyproxy for installing anyproxy
Now we can start the anyproxy - anyproxy --intercept
this will intercept all the requests. The proxy is started in port 8001 and the UI is started in 8002
Enable the proxy setting for Chrome
The HTTPS traffic will have an issue to decrypt the traffic - Now configure the anyproxy root certificate to Chrome browser - download the anyproxy root CA certificate from anyproxy ui - @
Import the root certificate to chrome - chrome://settings/?search=manage+certificates
Create a rule file to handle specific URL
module.exports = {
*beforeSendRequest(requestDetail) {
const localResponse = {
statusCode: 200,
header: { 'Content-Type': 'application/json' },
body: '{"hello": "this is proxy response"}'
};
if (requestDetail.url.indexOf('@') === 0) {
return {
response: localResponse
};
}
},
};
Start the anyproxy with rule file - anyproxy --intercept --rule sample.js
Access the URL - @ - this will show the proxy response
Now you can start pointing to the proxy server to test the front end functionalities while backend services are getting ready