Skip to content

Express.js Proxy

Route usertrax through your Express.js server using a lightweight HTTP proxy.

  1. Install dependencies

    Terminal-Fenster
    npm install express-http-proxy
  2. Add proxy routes

    const express = require("express");
    const proxy = require("express-http-proxy");
    const app = express();
    app.set("trust proxy", true);
    // Proxy the tracking script
    app.use(
    "/cvs.js",
    proxy("usertrax.io", {
    https: true,
    proxyReqPathResolver: () => "/cvs.js",
    }),
    );
    // Proxy the events endpoint
    app.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);
  3. Update your HTML

    <script>
    window.usertraxConfig = {
    endpoint: "/api/usertrax/events",
    };
    </script>
    <script src="/cvs.js" data-key="YOUR_API_KEY" defer></script>
  4. Deploy

    The proxy is active once your server is running with the updated configuration.

  1. Visit your website
  2. Open the Network tab
  3. Confirm requests go through your domain
  • 401 Unauthorized: Verify proxyReqOptDecorator forwards X-Auth-Key
  • Path conflicts: If /api/usertrax/events is already used, change the path in both Express and usertraxConfig.endpoint