Skip to content

Instantly share code, notes, and snippets.

@vwo-kb
Last active February 6, 2024 11:10
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 vwo-kb/fd2e7e590521551cce72f19808fbf261 to your computer and use it in GitHub Desktop.
Save vwo-kb/fd2e7e590521551cce72f19808fbf261 to your computer and use it in GitHub Desktop.
const express = require("express");
const fs = require("fs"),
https = require("https"),
http = require("http");
const { createProxyMiddleware } = require("http-proxy-middleware");
const dacdnDomain = "mydomain.com", dacdnPath = "/vwo"
const ancdnDomain = "mydomain.com", ancdnPath = ""
const yourWebsite = "mydomain.com"
/*
In this example, we have given the value of dacdnPath as "/vwo". This simply means VWO JS code will start proxying the requests to https://mydomain.com/vwo/
*/
/*
Here we have used vwo.yourWebsite.com as a subdomain to proxy the requests. It can be anything, be it a subdomain or a different domain altogether.
dacdnDomain → This is used to proxy all the A/B test/goals/funnels/surveys related requests.
ancdnDomain → This is used to proxy all the recordings/heatmaps/form-analytics related requests.
Unless you wish to have a different server to proxy the recordings/heatmaps/form-analytics related requests, keep the values of dacdnDomain & ancdnDomain the same.
*/
const app = express();
const options = {
changeOrigin: true,
router: {
// "yourDomain": "proxyto",
[`${ancdnDomain}${ancdnPath}/r1/`]: "https://r1.visualwebsiteoptimizer.com",
[`${ancdnDomain}${ancdnPath}/r2/`]: "https://r2.visualwebsiteoptimizer.com",
[`${ancdnDomain}${ancdnPath}/r3/`]: "https://r3.visualwebsiteoptimizer.com",
[`${dacdnDomain}${dacdnPath}/`]: "https://dev.visualwebsiteoptimizer.com",
[`${yourWebsite}/`]: "https:///yourwebsite.com",
"local.com/": "http://localhost:8000",
},
//If your account is configured with a non-US data center, replace 'r1', 'r2', 'r3',... with ‘r1eu01’, ‘r2eu01’, ‘r3eu01’,... for Europe and ‘r1as01’, ‘r2as01’, ‘r3as01’,... for India.//
onProxyReq: function (proxyReq, req, res) {
proxyReq.setHeader("X-Forwarded-VWO", dacdnDomain + dacdnPath);
proxyReq.setHeader("X-Forwarded-VWOInsights", ancdnDomain + ancdnPath); // this need not be be same as X-Forwarded-Host
},
pathRewrite: {
[`^${ancdnPath}/r1/`]: '',
[`^${ancdnPath}/r2/`]: '',
[`^${ancdnPath}/r3/`]: '',
[`^${dacdnPath}/`]: '',
},
};
//If your account is configured with a non-US data center, replace 'r1', 'r2', 'r3',... with ‘r1eu01’, ‘r2eu01’, ‘r3eu01’,... for Europe and ‘r1as01’, ‘r2as01’, ‘r3as01’,... for India.//
app.use("/", createProxyMiddleware(options));
https
.createServer(
{
key: fs.readFileSync("/Users/username/localhost.key"),
cert: fs.readFileSync("/Users/username/localhost.crt"),
},
app
)
.listen(443, () => {
console.log("Listening at :443...");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment