Express.js Proxy
Route usertrax through your Express.js server using a lightweight HTTP proxy.
Install dependencies
Terminal-Fenster npm install express-http-proxyAdd proxy routes
const express = require("express");const proxy = require("express-http-proxy");const app = express();app.set("trust proxy", true);// Proxy the tracking scriptapp.use("/cvs.js",proxy("usertrax.io", {https: true,proxyReqPathResolver: () => "/cvs.js",}),);// Proxy the events endpointapp.use("/api/usertrax/events",proxy("api.usertrax.io", {https: true,proxyReqPathResolver: () => "/api/events",proxyReqOptDecorator: (proxyReqOpts, srcReq) => {proxyReqOpts.headers["X-Auth-Key"] = srcReq.headers["x-auth-key"] || "";return proxyReqOpts;},}),);// Your other routes...app.listen(3000);Update your HTML
<script>window.usertraxConfig = {endpoint: "/api/usertrax/events",};</script><script src="/cvs.js" data-key="YOUR_API_KEY" defer></script>Deploy
The proxy is active once your server is running with the updated configuration.
Verification
Section titled “Verification”- Visit your website
- Open the Network tab
- Confirm requests go through your domain
Troubleshooting
Section titled “Troubleshooting”- 401 Unauthorized: Verify
proxyReqOptDecoratorforwardsX-Auth-Key - Path conflicts: If
/api/usertrax/eventsis already used, change the path in both Express andusertraxConfig.endpoint