Change server.js
Browse files- src/server.js +10 -39
src/server.js
CHANGED
|
@@ -2,7 +2,7 @@ const express = require('express');
|
|
| 2 |
const cors = require('cors');
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
const path = require('path');
|
| 5 |
-
require('dotenv').config({override: false});
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
|
|
@@ -49,55 +49,26 @@ app.get('/api/health', (req, res) => {
|
|
| 49 |
res.status(200).json({ status: 'OK', message: 'Server is running' });
|
| 50 |
});
|
| 51 |
|
| 52 |
-
//
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
endpoints: {
|
| 59 |
-
core: {
|
| 60 |
-
plants: '/api/plants',
|
| 61 |
-
surveys: '/api/surveys',
|
| 62 |
-
informants: '/api/informants',
|
| 63 |
-
locations: '/api/locations',
|
| 64 |
-
usageCategories: '/api/usage-categories',
|
| 65 |
-
conditions: '/api/conditions',
|
| 66 |
-
plantParts: '/api/plant-parts',
|
| 67 |
-
ingredients: '/api/ingredients',
|
| 68 |
-
animalTypes: '/api/animal-types'
|
| 69 |
-
},
|
| 70 |
-
features: {
|
| 71 |
-
search: '/api/search?q=neem',
|
| 72 |
-
searchPlants: '/api/search/plants?name=tulsi',
|
| 73 |
-
searchByCondition: '/api/search/condition?condition=fever',
|
| 74 |
-
advancedFilter: '/api/search/filter (POST)',
|
| 75 |
-
dashboard: '/api/stats/dashboard',
|
| 76 |
-
plantStats: '/api/stats/plants',
|
| 77 |
-
surveyStats: '/api/stats/surveys',
|
| 78 |
-
locationStats: '/api/stats/by-location'
|
| 79 |
-
}
|
| 80 |
-
}
|
| 81 |
});
|
| 82 |
-
}
|
| 83 |
|
| 84 |
// Error handling middleware (must be last)
|
| 85 |
app.use(errorHandler);
|
| 86 |
|
| 87 |
-
|
| 88 |
-
if (process.env.NODE_ENV === 'production') {
|
| 89 |
-
app.use(express.static(path.join(__dirname, '../frontend/build')));
|
| 90 |
-
app.get('*', (req, res) => {
|
| 91 |
-
res.sendFile(path.join(__dirname, '../frontend/build', 'index.html'));
|
| 92 |
-
});
|
| 93 |
-
} else {
|
| 94 |
// 404 handler for dev
|
| 95 |
app.use((req, res) => {
|
| 96 |
res.status(404).json({ error: 'Route not found' });
|
| 97 |
});
|
| 98 |
}
|
| 99 |
|
| 100 |
-
const PORT = process.env.PORT || 5000;
|
| 101 |
|
| 102 |
app.listen(PORT, () => {
|
| 103 |
console.log(`Server is running on port ${PORT}`);
|
|
|
|
| 2 |
const cors = require('cors');
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
const path = require('path');
|
| 5 |
+
require('dotenv').config({ override: false });
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
|
|
|
|
| 49 |
res.status(200).json({ status: 'OK', message: 'Server is running' });
|
| 50 |
});
|
| 51 |
|
| 52 |
+
// Serve React frontend in production (before error handler)
|
| 53 |
+
if (process.env.NODE_ENV === 'production') {
|
| 54 |
+
app.use(express.static(path.join(__dirname, '../frontend/build')));
|
| 55 |
+
// All non-API routes serve the React app
|
| 56 |
+
app.get(/^(?!\/api).*/, (req, res) => {
|
| 57 |
+
res.sendFile(path.join(__dirname, '../frontend/build', 'index.html'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
});
|
| 59 |
+
}
|
| 60 |
|
| 61 |
// Error handling middleware (must be last)
|
| 62 |
app.use(errorHandler);
|
| 63 |
|
| 64 |
+
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
// 404 handler for dev
|
| 66 |
app.use((req, res) => {
|
| 67 |
res.status(404).json({ error: 'Route not found' });
|
| 68 |
});
|
| 69 |
}
|
| 70 |
|
| 71 |
+
const PORT = process.env.NODE_ENV === 'production' ? 7860 : (process.env.PORT || 5000);
|
| 72 |
|
| 73 |
app.listen(PORT, () => {
|
| 74 |
console.log(`Server is running on port ${PORT}`);
|