Last active
February 6, 2024 11:29
-
-
Save vwo-kb/09099fb567f9ed185096091dd755200a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const fs = require("fs"), | |
https = require("https"), | |
http = require("http"); | |
const { createProxyMiddleware } = require("http-proxy-middleware"); | |
const dacdnDomain = "sub.mydomain.com", dacdnPath = "" | |
const ancdnDomain = "sub.mydomain.com", ancdnPath = "" | |
const yourWebsite = "mydomain.com" | |
/* | |
Here we have used sub.mydomain.com as a subdomain to proxy the requests. It can be anything, be it a subdomain or a different domain altogether. In this example, VWO JS code will start proxying the requests to https://sub.mydomain.com/ | |
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: { | |
// "mydomain.com": "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