gaialive commited on
Commit
a510753
·
verified ·
1 Parent(s): 7f05a1c

Upload index.js

Browse files
Files changed (1) hide show
  1. server/index.js +31 -35
server/index.js CHANGED
@@ -47,54 +47,36 @@ app.use(express.json());
47
  // Serve static files from the client build in production
48
  if (process.env.NODE_ENV === 'production') {
49
  app.use(express.static('/usr/share/nginx/html'));
50
-
51
- // Health check endpoint for containerized environments
52
- app.get('/health', (req, res) => {
53
- res.status(200).json({
54
- status: 'OK',
55
- timestamp: new Date().toISOString(),
56
- uptime: process.uptime()
57
- });
58
  });
59
-
60
- // Serve the React app for the root route
61
- app.get('/', (req, res) => {
 
 
62
  res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
63
- });
64
-
65
- // In production, serve the React app for any non-API routes
66
- app.get('*', (req, res) => {
67
- if (!req.path.startsWith('/api/')) {
68
- res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
69
- } else {
70
- res.status(404).json({ error: 'API endpoint not found' });
71
- }
72
- });
73
- } else {
74
- // Development endpoints
75
- app.get('/', (req, res) => {
76
  res.json({
77
  message: 'BioNexus Hub API',
78
  version: '1.0.0',
79
  timestamp: new Date().toISOString()
80
  });
81
- });
82
-
83
- // Health check endpoint for development
84
- app.get('/health', (req, res) => {
85
- res.status(200).json({
86
- status: 'OK',
87
- timestamp: new Date().toISOString(),
88
- uptime: process.uptime()
89
- });
90
- });
91
- }
92
 
93
  app.get('/api/news', async (req, res) => {
94
  try {
95
  const news = await News.getAll();
96
  res.json(news);
97
  } catch (error) {
 
98
  res.status(500).json({ error: 'Failed to fetch news' });
99
  }
100
  });
@@ -108,6 +90,7 @@ app.get('/api/news/:id', async (req, res) => {
108
  }
109
  res.json(newsItem);
110
  } catch (error) {
 
111
  res.status(500).json({ error: 'Failed to fetch news item' });
112
  }
113
  });
@@ -117,6 +100,7 @@ app.get('/api/datasets', async (req, res) => {
117
  const datasets = await Dataset.getAll();
118
  res.json(datasets);
119
  } catch (error) {
 
120
  res.status(500).json({ error: 'Failed to fetch datasets' });
121
  }
122
  });
@@ -130,10 +114,22 @@ app.get('/api/datasets/:id', async (req, res) => {
130
  }
131
  res.json(dataset);
132
  } catch (error) {
 
133
  res.status(500).json({ error: 'Failed to fetch dataset' });
134
  }
135
  });
136
 
 
 
 
 
 
 
 
 
 
 
 
137
  app.listen(PORT, '0.0.0.0', () => {
138
  console.log(`BioNexus Hub server running on port ${PORT}`);
139
  });
 
47
  // Serve static files from the client build in production
48
  if (process.env.NODE_ENV === 'production') {
49
  app.use(express.static('/usr/share/nginx/html'));
50
+ }
51
+
52
+ // Health check endpoint
53
+ app.get('/health', (req, res) => {
54
+ res.status(200).json({
55
+ status: 'OK',
56
+ timestamp: new Date().toISOString(),
57
+ uptime: process.uptime()
58
  });
59
+ });
60
+
61
+ app.get('/', (req, res) => {
62
+ if (process.env.NODE_ENV === 'production') {
63
+ // In production, serve the React app
64
  res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
65
+ } else {
 
 
 
 
 
 
 
 
 
 
 
 
66
  res.json({
67
  message: 'BioNexus Hub API',
68
  version: '1.0.0',
69
  timestamp: new Date().toISOString()
70
  });
71
+ }
72
+ });
 
 
 
 
 
 
 
 
 
73
 
74
  app.get('/api/news', async (req, res) => {
75
  try {
76
  const news = await News.getAll();
77
  res.json(news);
78
  } catch (error) {
79
+ console.error('Error fetching news:', error);
80
  res.status(500).json({ error: 'Failed to fetch news' });
81
  }
82
  });
 
90
  }
91
  res.json(newsItem);
92
  } catch (error) {
93
+ console.error('Error fetching news item:', error);
94
  res.status(500).json({ error: 'Failed to fetch news item' });
95
  }
96
  });
 
100
  const datasets = await Dataset.getAll();
101
  res.json(datasets);
102
  } catch (error) {
103
+ console.error('Error fetching datasets:', error);
104
  res.status(500).json({ error: 'Failed to fetch datasets' });
105
  }
106
  });
 
114
  }
115
  res.json(dataset);
116
  } catch (error) {
117
+ console.error('Error fetching dataset:', error);
118
  res.status(500).json({ error: 'Failed to fetch dataset' });
119
  }
120
  });
121
 
122
+ // In production, serve the React app for any non-API routes
123
+ if (process.env.NODE_ENV === 'production') {
124
+ app.get('*', (req, res) => {
125
+ if (!req.path.startsWith('/api/') && req.path !== '/health') {
126
+ res.sendFile(path.resolve('/usr/share/nginx/html/index.html'));
127
+ } else {
128
+ res.status(404).json({ error: 'API endpoint not found' });
129
+ }
130
+ });
131
+ }
132
+
133
  app.listen(PORT, '0.0.0.0', () => {
134
  console.log(`BioNexus Hub server running on port ${PORT}`);
135
  });