Skip to content

Commit 53d5fdb

Browse files
committed
missing file
1 parent b4c8dba commit 53d5fdb

File tree

1 file changed

+79
-0
lines changed
  • web-integrations/javascript-sdk/react-client-side

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require('dotenv').config({ path: '../../../.env' });
2+
3+
console.log('process.env', process.env);
4+
5+
const fs = require('fs');
6+
const path = require('path');
7+
const express = require('express');
8+
9+
const app = express();
10+
11+
const port = process.env.PORT || 3034;
12+
13+
// Healthcheck endpoint for Kubernetes probes - must be before all other routes and middleware
14+
app.get('/ops/healthcheck', (req, res) => {
15+
console.log('Healthcheck endpoint hit');
16+
res.type('text/plain');
17+
res.status(200).send('healthy\n');
18+
return; // Explicit return to prevent further processing
19+
});
20+
21+
// Helper function to serve index.html with environment variable replacement
22+
function serveIndexHtml(req, res) {
23+
// Try build directory first (production), then public (development)
24+
let indexPath = path.join(__dirname, 'build', 'index.html');
25+
if (!fs.existsSync(indexPath)) {
26+
indexPath = path.join(__dirname, 'public', 'index.html');
27+
}
28+
29+
let html = fs.readFileSync(indexPath, 'utf8');
30+
31+
// Replace environment variable placeholders
32+
const uidJsSdkUrl = process.env.REACT_APP_UID_JS_SDK_URL || 'https://cdn.integ.uidapi.com/uid2-sdk-4.0.1.js';
33+
const publicUrl = process.env.PUBLIC_URL || '';
34+
35+
// Replace placeholders (using __PLACEHOLDER__ format to avoid React build processing)
36+
html = html.replace(/__UID_JS_SDK_URL_PLACEHOLDER__/g, uidJsSdkUrl);
37+
html = html.replace(/__PUBLIC_URL_PLACEHOLDER__/g, publicUrl);
38+
39+
// Debug: log if replacement happened
40+
if (html.includes('__UID_JS_SDK_URL_PLACEHOLDER__')) {
41+
console.warn('Warning: Placeholder __UID_JS_SDK_URL_PLACEHOLDER__ was not replaced in', indexPath);
42+
}
43+
44+
res.send(html);
45+
}
46+
47+
// Route handler for index - must be before static middleware
48+
app.get('/', serveIndexHtml);
49+
50+
// Serve static files from build directory (production) or public (development)
51+
const buildPath = path.join(__dirname, 'build');
52+
const publicPath = path.join(__dirname, 'public');
53+
54+
if (fs.existsSync(buildPath)) {
55+
// Production: serve from build directory (but exclude index.html to use our handler)
56+
app.use(express.static(buildPath, {
57+
index: false // Don't serve index.html automatically
58+
}));
59+
} else {
60+
// Development: serve from public directory
61+
app.use(express.static(publicPath, {
62+
index: false // Don't serve index.html automatically
63+
}));
64+
}
65+
66+
// Catch-all handler for React Router (must be last, but excludes /ops/* routes)
67+
app.get('*', (req, res, next) => {
68+
// Don't serve index.html for /ops/* routes
69+
if (req.path.startsWith('/ops/')) {
70+
console.log('Catch-all skipping /ops/ route:', req.path);
71+
return next();
72+
}
73+
serveIndexHtml(req, res);
74+
});
75+
76+
app.listen(port, () => {
77+
console.log(`Example app listening at http://localhost:${port}`);
78+
});
79+

0 commit comments

Comments
 (0)