const express = require('express'); const fs = require('fs'); const path = require('path'); const app = express(); const port = 3000; app.use(express.static('public')); // Serve static files from the 'public' directory // Endpoint to get the most recent image app.get('/most-recent-image', (req, res) => { const imagesDirectory = path.join(__dirname, 'images'); fs.readdir(imagesDirectory, (err, files) => { if (err) { return res.status(500).send('Unable to scan directory'); } let mostRecentImage = null; let maxDate = Date.now(); files.forEach(file => { const filePath = path.join(imagesDirectory, file); const stats = fs.statSync(filePath); if (stats.isFile() && /\.(jpg|jpeg|png|gif)$/i.test(file)) { const currentDate = new Date(stats.mtime).getTime(); if (currentDate < maxDate) { mostRecentImage = file; maxDate = currentDate; } } }); if (mostRecentImage) { res.json({ imagePath: `/images/${mostRecentImage}` }); } else { res.status(404).send('No images found'); } }); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });