gaialive commited on
Commit
67d10f0
·
verified ·
1 Parent(s): eaeb842

Upload index.js

Browse files
Files changed (1) hide show
  1. server/index.js +23 -18
server/index.js CHANGED
@@ -50,6 +50,11 @@ app.use(express.json());
50
  // Serve static files from the client build in production
51
  if (process.env.NODE_ENV === 'production') {
52
  app.use(express.static('/usr/share/nginx/html'));
 
 
 
 
 
53
  }
54
 
55
  // Health check endpoint
@@ -61,19 +66,7 @@ app.get('/health', (req, res) => {
61
  });
62
  });
63
 
64
- app.get('/', (req, res) => {
65
- if (process.env.NODE_ENV === 'production') {
66
- // In production, serve the React app
67
- res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
68
- } else {
69
- res.json({
70
- message: 'BioNexus Hub API',
71
- version: '1.0.0',
72
- timestamp: new Date().toISOString()
73
- });
74
- }
75
- });
76
-
77
  app.get('/api/news', async (req, res) => {
78
  try {
79
  const news = await News.getAll();
@@ -122,14 +115,26 @@ app.get('/api/datasets/:id', async (req, res) => {
122
  }
123
  });
124
 
125
- // In production, serve the React app for any non-API routes
126
  if (process.env.NODE_ENV === 'production') {
 
127
  app.get('*', (req, res) => {
128
- if (!req.path.startsWith('/api/') && req.path !== '/health') {
129
- res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
130
- } else {
131
- res.status(404).json({ error: 'API endpoint not found' });
132
  }
 
 
 
 
 
 
 
 
 
 
 
 
133
  });
134
  }
135
 
 
50
  // Serve static files from the client build in production
51
  if (process.env.NODE_ENV === 'production') {
52
  app.use(express.static('/usr/share/nginx/html'));
53
+
54
+ // Explicitly serve the main index.html file at the root route
55
+ app.get('/', (req, res) => {
56
+ res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
57
+ });
58
  }
59
 
60
  // Health check endpoint
 
66
  });
67
  });
68
 
69
+ // API routes
 
 
 
 
 
 
 
 
 
 
 
 
70
  app.get('/api/news', async (req, res) => {
71
  try {
72
  const news = await News.getAll();
 
115
  }
116
  });
117
 
118
+ // Serve the React app for all non-API routes in production
119
  if (process.env.NODE_ENV === 'production') {
120
+ // For any non-API routes, serve the React app
121
  app.get('*', (req, res) => {
122
+ // Don't serve the React app for API routes or health check
123
+ if (req.path.startsWith('/api/') || req.path === '/health') {
124
+ return res.status(404).json({ error: 'API endpoint not found' });
 
125
  }
126
+
127
+ // Serve the React app for all other routes
128
+ res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
129
+ });
130
+ } else {
131
+ // In development, serve API info at root
132
+ app.get('/', (req, res) => {
133
+ res.json({
134
+ message: 'BioNexus Hub API',
135
+ version: '1.0.0',
136
+ timestamp: new Date().toISOString()
137
+ });
138
  });
139
  }
140