update readme
This commit is contained in:
23
files/image_refresh/Dockerfile
Normal file
23
files/image_refresh/Dockerfile
Normal file
@ -0,0 +1,23 @@
|
||||
# Use an official Node runtime as a parent image
|
||||
FROM node:14
|
||||
|
||||
# Create and change to the app directory
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Copy package.json and package-lock.json
|
||||
COPY package*.json ./
|
||||
|
||||
# Install any needed packages specified in package.json
|
||||
RUN npm install
|
||||
|
||||
# Bundle app source inside Docker container
|
||||
COPY . .
|
||||
|
||||
# Make port 3000 available to the world outside this container
|
||||
EXPOSE 3000
|
||||
|
||||
# Define environment variable
|
||||
ENV NAME World
|
||||
|
||||
# Run app.py when the container launches
|
||||
CMD ["node", "server.js"]
|
||||
12
files/image_refresh/package.json
Normal file
12
files/image_refresh/package.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "docker_web_app",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple Docker Web app",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.17.1"
|
||||
}
|
||||
}
|
||||
39
files/image_refresh/server.js
Normal file
39
files/image_refresh/server.js
Normal 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}`);
|
||||
});
|
||||
Reference in New Issue
Block a user