| const express = require('express');
|
| const cors = require('cors');
|
| const bodyParser = require('body-parser');
|
| const path = require('path');
|
| require('dotenv').config({ override: false });
|
|
|
| const app = express();
|
|
|
|
|
| const requestLogger = require('./middleware/requestLogger');
|
| const errorHandler = require('./middleware/errorHandler');
|
|
|
|
|
| app.use(cors());
|
| app.use(bodyParser.json());
|
| app.use(bodyParser.urlencoded({ extended: true }));
|
| app.use(requestLogger);
|
|
|
|
|
| const plantRoutes = require('./routes/plantRoutes');
|
| const surveyRoutes = require('./routes/surveyRoutes');
|
| const informantRoutes = require('./routes/informantRoutes');
|
| const locationRoutes = require('./routes/locationRoutes');
|
| const usageCategoryRoutes = require('./routes/usageCategoryRoutes');
|
| const conditionRoutes = require('./routes/conditionRoutes');
|
| const plantPartRoutes = require('./routes/plantPartRoutes');
|
| const ingredientRoutes = require('./routes/ingredientRoutes');
|
| const animalTypeRoutes = require('./routes/animalTypeRoutes');
|
| const searchRoutes = require('./routes/searchRoutes');
|
| const statsRoutes = require('./routes/statsRoutes');
|
| const mediaRoutes = require('./routes/mediaRoutes');
|
|
|
|
|
| app.use('/api/plants', plantRoutes);
|
| app.use('/api/surveys', surveyRoutes);
|
| app.use('/api/informants', informantRoutes);
|
| app.use('/api/locations', locationRoutes);
|
| app.use('/api/usage-categories', usageCategoryRoutes);
|
| app.use('/api/conditions', conditionRoutes);
|
| app.use('/api/plant-parts', plantPartRoutes);
|
| app.use('/api/ingredients', ingredientRoutes);
|
| app.use('/api/animal-types', animalTypeRoutes);
|
| app.use('/api/search', searchRoutes);
|
| app.use('/api/stats', statsRoutes);
|
| app.use('/api/media', mediaRoutes);
|
|
|
|
|
| app.get('/api/health', (req, res) => {
|
| res.status(200).json({ status: 'OK', message: 'Server is running' });
|
| });
|
|
|
|
|
| app.get('/api/debug/images', (req, res) => {
|
| const fs = require('fs');
|
| const imgDir = path.join(__dirname, '../frontend/build/images');
|
| try {
|
| const files = fs.existsSync(imgDir) ? fs.readdirSync(imgDir) : [];
|
| res.json({ path: imgDir, exists: fs.existsSync(imgDir), files });
|
| } catch (e) {
|
| res.json({ error: e.message });
|
| }
|
| });
|
|
|
|
|
| const fs = require('fs');
|
| const buildPath = path.join(__dirname, '../frontend/build');
|
| const indexPath = path.join(buildPath, 'index.html');
|
|
|
| console.log(`Frontend build path: ${buildPath}`);
|
| console.log(`index.html exists: ${fs.existsSync(indexPath)}`);
|
|
|
| if (fs.existsSync(indexPath)) {
|
| app.use(express.static(buildPath));
|
| app.use((req, res, next) => {
|
| if (req.path.startsWith('/api')) return next();
|
| res.sendFile(indexPath, (err) => {
|
| if (err) {
|
| console.error('sendFile error:', err.message);
|
| next(err);
|
| }
|
| });
|
| });
|
| }
|
|
|
|
|
| app.use(errorHandler);
|
|
|
|
|
| app.use((req, res) => {
|
| res.status(404).json({ error: 'Route not found' });
|
| });
|
|
|
| const PORT = process.env.NODE_ENV === 'production' ? 7860 : (process.env.PORT || 5000);
|
|
|
| app.listen(PORT, () => {
|
| console.log(`Server is running on port ${PORT}`);
|
| console.log(`Health check: http://localhost:${PORT}/api/health`);
|
| });
|
|
|
| module.exports = app;
|
|
|