Download src/server.js from Mr-Thop/DigitalDB: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/Mr-Thop/DigitalDB/resolve/main/src/server.js
- Command line
-
hf download hf://spaces/Mr-Thop/DigitalDB/src/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Mr-Thop/DigitalDB/resolve/main/src/server.js
3.45 kB
| 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(); | |
| // Import middleware | |
| const requestLogger = require('./middleware/requestLogger'); | |
| const errorHandler = require('./middleware/errorHandler'); | |
| // Middleware | |
| app.use(cors()); | |
| app.use(bodyParser.json()); | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| app.use(requestLogger); // Log all requests | |
| // Import routes | |
| 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'); | |
| // Use routes | |
| 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); | |
| // Health check endpoint | |
| app.get('/api/health', (req, res) => { | |
| res.status(200).json({ status: 'OK', message: 'Server is running' }); | |
| }); | |
| // Debug: list files in frontend/build/images | |
| 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 }); | |
| } | |
| }); | |
| // Serve React frontend - always attempt if build exists | |
| 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); | |
| } | |
| }); | |
| }); | |
| } | |
| // Error handling middleware (must be last) | |
| app.use(errorHandler); | |
| // 404 fallback | |
| 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; | |