all working i think

This commit is contained in:
2025-07-28 06:21:45 -07:00
parent b740ba9991
commit 211930cde3
30 changed files with 900 additions and 117 deletions

View File

@ -0,0 +1,39 @@
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}`);
});