Skip to content

Commit 6ef5860

Browse files
committed
Fix MERN stack integration and URL routing between React frontend and backends
Environment Configuration Updates: - Updated MONGO_URI to use environment variables in Node.js app.js - Fixed REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL to point to localhost:5001 (Node.js) - Added proper quotes and escaping for MongoDB connection strings in .env files - Fixed malformed MONGO_URI in .env.example file Node.js Backend Improvements: - Changed default port from 5000 to 5001 to avoid macOS AirPlay conflict - Added root endpoint (GET /) with server status and available endpoints - Enhanced build_and_run_local.sh script with proper environment variable loading - Added node-fetch dependency to package.json automatically React Frontend URL Routing Fixes: - Fixed inconsistent routing for 'MongoDB API accessing Oracle Database' option - GET requests now correctly use REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL (localhost:5001) - POST requests already correctly routed to respective backends - Updated default fallback URLs in component constants Spring Boot Backend: - Already properly configured with CORS and environment variables - Endpoints remain on localhost:8080 as expected URL Mapping Summary: - 'SQL' option → Spring Boot (localhost:8080/accounts/*) - 'MongoDB API accessing Oracle Database' → Node.js (localhost:5001/api/*) - 'MongoDB API' option → Spring Boot (localhost:8080/accounts/*) Build Scripts: - All three services now have build_and_run_local.sh scripts - Scripts source environment variables from financial/.env - Proper port assignments: React(3000), Spring Boot(8080), Node.js(5001) The full stack now correctly routes between React frontend and both backend services with proper environment variable management and no port conflicts.
1 parent 38634ad commit 6ef5860

File tree

5 files changed

+86
-22
lines changed

5 files changed

+86
-22
lines changed

financial/.env.example

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
# Application Configuration
21

3-
export REACT_APP_MERN_MONGODB_SERVICE_URL=http://localhost:8080
4-
export REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL=http://localhost:8080
2+
# Database Configuration for Spring Boot Services
3+
export SPRING_DATASOURCE_URL=jdbc:oracle:thin:@financialdb_high?TNS_ADMIN=/Users/pparkins/Downloads/Wallet_financialdb
4+
export SPRING_DATASOURCE_USERNAME=financial
5+
export SPRING_DATASOURCE_PASSWORD=Welcome12345
6+
7+
# MongoDB Configuration for MERN Stack (Oracle JSON Duality View)
8+
export MONGO_URI="mongodb://financial:mypassword@asdf-FINANCIALDB.adb.eu-frankfurt-1.oraclecloudapps.com:27017/financial?authMechanism=PLAIN&authSource=\$external&ssl=true&retryWrites=false&loadBalanced=true"
9+
10+
# Application Configuration
511
export REACT_APP_MERN_SQL_ORACLE_SERVICE_URL=http://localhost:8080
12+
export REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL=http://localhost:5001
13+
export REACT_APP_MERN_MONGODB_SERVICE_URL=http://localhost:8080
614

715
export REACT_APP_MICROTX_TRANSFER_SERVICE_URL=http://localhost:8080/accounts
816
export REACT_APP_MICROTX_ACCOUNT_SERVICE_URL=http://localhost:8080/accounts
@@ -16,11 +24,6 @@ export REACT_APP_AIAGENT_VECTOR_ADVISOR_SERVICE_URL=http://localhost:8080/accoun
1624
export REACT_APP_SPEECH_SELECTAI_QUERY_SERVICE_URL=http://localhost:8080/accounts
1725
export REACT_APP_ORDS_BASE_URL=http://localhost:8080/accounts
1826

19-
# Database Configuration for Spring Boot Services
20-
export SPRING_DATASOURCE_URL=jdbc:oracle:thin:@financialdb_high?TNS_ADMIN=/Users/pparkins/Downloads/Wallet_financialdb
21-
export SPRING_DATASOURCE_USERNAME=financial
22-
export SPRING_DATASOURCE_PASSWORD=Welcome12345
23-
2427
# Display all set environment variables
2528
echo "=== Environment Variables Set ==="
2629
echo "REACT_APP_MERN_MONGODB_SERVICE_URL: $REACT_APP_MERN_MONGODB_SERVICE_URL"
@@ -40,4 +43,5 @@ echo "REACT_APP_ORDS_BASE_URL: $REACT_APP_ORDS_BASE_URL"
4043
echo "SPRING_DATASOURCE_URL: $SPRING_DATASOURCE_URL"
4144
echo "SPRING_DATASOURCE_USERNAME: $SPRING_DATASOURCE_USERNAME"
4245
echo "SPRING_DATASOURCE_PASSWORD: $SPRING_DATASOURCE_PASSWORD"
46+
echo "MONGO_URI: $MONGO_URI"
4347
echo "=== All environment variables have been set successfully! ==="

financial/mongodb-mern-bank-account/mern-stack/app.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const fetch = require('node-fetch');
66

77
// Initialize Express app
88
const app = express();
9-
const PORT = 5000;
9+
const PORT = process.env.PORT || 5001; // Changed from 5000 to 5001 to avoid AirPlay conflict
1010

1111
// Middleware
1212
app.use(bodyParser.json());
1313
app.use(cors());
1414

1515
// MongoDB connection (using Oracle JSON Duality View as MongoDB API)
16-
const MONGO_URI = 'mongodb://financial:Welcome12345@IJ1TYZIR3WPWLPE-FINANCIALDB.adb.eu-frankfurt-1.oraclecloudapps.com:27017/financial?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true';
16+
const MONGO_URI = process.env.MONGO_URI || 'mongodb://financial:Welcome12345@IJ1TYZIR3WPWLPE-FINANCIALDB.adb.eu-frankfurt-1.oraclecloudapps.com:27017/financial?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true';
1717
mongoose
1818
.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
1919
.then(() => console.log('Connected to Oracle JSON Duality View via MongoDB API'))
@@ -33,6 +33,29 @@ const accountSchema = new mongoose.Schema({
3333
// Create the model
3434
const Account = mongoose.model('accounts_dv', accountSchema);
3535

36+
// Root endpoint to verify server is running
37+
app.get('/', (req, res) => {
38+
res.json({
39+
message: 'MERN Stack Backend API is running!',
40+
status: 'OK',
41+
timestamp: new Date().toISOString(),
42+
endpoints: {
43+
'GET /': 'This status endpoint',
44+
'GET /api/accounts': 'Get all accounts',
45+
'POST /api/accounts': 'Create new account',
46+
'GET /api/accounts/:id': 'Get account by ID',
47+
'PUT /api/accounts/:id': 'Update account by ID',
48+
'DELETE /api/accounts/:id': 'Delete account by ID',
49+
'POST /accounts/api/accounts': 'Proxy endpoint for account creation'
50+
},
51+
database: {
52+
type: 'Oracle Database via MongoDB API',
53+
collection: 'accounts_dv',
54+
connection: MONGO_URI ? 'Environment variable configured' : 'Using fallback configuration'
55+
}
56+
});
57+
});
58+
3659
// CRUD Endpoints
3760

3861
// Create an account
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
echo "=== Building and Running MERN Stack Node.js Backend Locally ==="
4+
5+
# Step 1: Source environment variables from financial/.env
6+
echo "1. Sourcing environment variables from ../../.env"
7+
if [ -f "../../.env" ]; then
8+
source ../../.env
9+
echo " Environment variables loaded successfully!"
10+
echo " MONGO_URI: $MONGO_URI"
11+
else
12+
echo " Warning: ../../.env file not found, using default configuration"
13+
fi
14+
15+
# Step 2: Install dependencies if node_modules doesn't exist
16+
echo ""
17+
echo "2. Checking dependencies..."
18+
if [ ! -d "node_modules" ]; then
19+
echo " Installing npm dependencies..."
20+
npm install
21+
if [ $? -ne 0 ]; then
22+
echo " npm install failed! Exiting..."
23+
exit 1
24+
fi
25+
echo " Dependencies installed successfully!"
26+
else
27+
echo " Dependencies already installed, skipping npm install"
28+
fi
29+
30+
# Step 3: Start the Node.js server
31+
echo ""
32+
echo "3. Starting MERN Stack Node.js Backend..."
33+
echo " Server will be available at: http://localhost:5001"
34+
echo " MongoDB API endpoints:"
35+
echo " - POST/GET /api/accounts"
36+
echo " - GET/PUT/DELETE /api/accounts/:id"
37+
echo " Press Ctrl+C to stop the server"
38+
echo ""
39+
40+
node app.js

financial/react-frontend/src/pages/Accounts.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ const PanelSection = styled.div`
173173
}
174174
`;
175175

176-
const REACT_APP_MERN_MONGODB_SERVICE_URL = process.env.REACT_APP_MERN_MONGODB_SERVICE_URL || 'http://localhost:8080';
177-
const REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL =
178-
process.env.REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL || 'http://localhost:5000';
179176
const REACT_APP_MERN_SQL_ORACLE_SERVICE_URL =
180-
process.env.REACT_APP_MERN_SQL_ORACLE_SERVICE_URL || 'http://localhost:6000';
177+
process.env.REACT_APP_MERN_SQL_ORACLE_SERVICE_URL || 'http://localhost:8080';
178+
const REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL =
179+
process.env.REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL || 'http://localhost:5001';
180+
const REACT_APP_MERN_MONGODB_SERVICE_URL = process.env.REACT_APP_MERN_MONGODB_SERVICE_URL || 'http://localhost:8080';
181181

182182
const Accounts = () => {
183183
const [formData, setFormData] = useState({
@@ -206,10 +206,10 @@ const Accounts = () => {
206206
let postUrl;
207207
if (formData.writeOption === 'SQL') {
208208
postUrl = `${REACT_APP_MERN_SQL_ORACLE_SERVICE_URL}/accounts/api/accounts`;
209+
} else if (formData.writeOption === 'MongoDB API accessing Oracle Database') {
210+
postUrl = `${REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL}/api/accounts`;
209211
} else if (formData.writeOption === 'MongoDB API') {
210212
postUrl = `${REACT_APP_MERN_MONGODB_SERVICE_URL}/accounts/api/accounts`;
211-
} else if (formData.writeOption === 'MongoDB API accessing Oracle Database') {
212-
postUrl = `${REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL}/accounts/api/accounts`;
213213
}
214214

215215
const payload = {
@@ -261,7 +261,7 @@ const Accounts = () => {
261261
} else if (formData.readOption === 'MongoDB API') {
262262
fetchUrl = `${REACT_APP_MERN_MONGODB_SERVICE_URL}/accounts/accounts`;
263263
} else if (formData.readOption === 'MongoDB API accessing Oracle Database') {
264-
fetchUrl = `${REACT_APP_MERN_SQL_ORACLE_SERVICE_URL}/accounts/accounts`;
264+
fetchUrl = `${REACT_APP_MERN_MONGODB_JSONDUALITY_ORACLE_SERVICE_URL}/api/accounts`;
265265
}
266266

267267
try {

financial/sql/mongodb-jsonduality.sql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ BEGIN
44
END;
55
/
66

7-
mongodb://[user:password@]IJ1TYZIR3WPWLPE-FINANCIALDB.adb.eu-frankfurt-1.oraclecloudapps.com:27017/[user]?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true
7+
-- example URL format... mongodb://[user:password@]IJ1TYZIR3WPWLPE-FINANCIALDB.adb.eu-frankfurt-1.oraclecloudapps.com:27017/[user]?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true
88

9-
10-
11-
12-
--From https://docs.oracle.com/en/database/oracle/oracle-database/23/jsnvu/json-relational-duality-developers-guide.pdf
9+
-- also see https://docs.oracle.com/en/database/oracle/oracle-database/23/jsnvu/json-relational-duality-developers-guide.pdf
1310

1411

1512
--ACCOUNT_ID NOT NULL NUMBER(19)

0 commit comments

Comments
 (0)