Certainly! I'll help you create a modern and futuristic website that meets all your requirements. We'll develop a web application with the following features:
- Black background with a modern and futuristic design.
- Ability to upload pictures from your computer.
- Display pictures in a skippable slideshow with left and right arrows.
- When navigating, the new picture comes forward while the previous one remains slightly visible.
- Each picture has an interactive 5-star rating system saved in the database.
- Ability to write a review for each picture, also saved in the database.
- An upload button to add more photos to the slideshow.
We'll use HTML, CSS, and JavaScript for the front end, and Node.js with Express.js for the back end to handle file uploads and data storage. We'll also use JSON files to store data locally since you want to run it locally without setting up a full database system.
Table of Contents
- Project Setup
- Front-End Development
- Back-End Development
- Running the Application Locally
- Conclusion
Project Setup
-
Create a Project Folder
Create a new folder for your project, e.g., photo-slideshow.
-
Initialize Node.js
Open the folder in Visual Studio Code (VS Code) and open the terminal. Initialize a Node.js project:
npm init -y
-
Install Dependencies
Install the necessary packages:
npm install express multer ejs
- express: Web framework for Node.js.
- multer: Middleware for handling file uploads.
- ejs: Embedded JavaScript templating (optional, if we decide to use templates).
-
Project Structure
Create the following folders and files:
photo-slideshow/
├── public/
│ ├── css/
│ │ └── styles.css
│ ├── js/
│ │ └── script.js
│ └── uploads/ (to store uploaded images)
├── views/
│ └── index.html
├── data/
│ └── images.json (to store image data)
├── app.js
└── package.json
Front-End Development
HTML Structure
Create index.html inside the views folder.
<!-- views/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Photo Slideshow</title>
<link rel="stylesheet" href="/css/styles.css" />
<script src="/js/script.js" defer></script>
</head>
<body>
<div class="container">
<div class="slideshow">
<button class="arrow left-arrow" id="prevBtn">❮</button>
<div class="slides" id="slides">
<!-- Images will be injected here -->
</div>
<button class="arrow right-arrow" id="nextBtn">❯</button>
</div>
<div class="upload-section">
<h2>Upload a New Picture</h2>
<form id="uploadForm" action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" required />
<button type="submit">Upload Picture</button>
</form>
</div>
</div>
</body>
</html>
CSS Styling
Create styles.css inside public/css/.
/* public/css/styles.css */
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #000;
color: #fff;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.slideshow {
position: relative;
margin-bottom: 50px;
}
.slides {
position: relative;
overflow: hidden;
height: 500px;
}
.slides .slide {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 1s;
height: 100%;
width: auto;
max-width: 100%;
}
.slides .slide.active {
opacity: 1;
}
.slides .slide.previous {
opacity: 0.5;
}
.arrow {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: #fff;
background-color: rgba(0,0,0,0.5);
border: none;
font-size: 24px;
user-select: none;
}
.left-arrow {
left: 0;
}
.right-arrow {
right: 0;
}
.upload-section {
text-align: center;
}
.upload-section h2 {
margin-bottom: 20px;
}
.upload-section form {
display: inline-block;
}
.upload-section input[type="file"] {
margin-bottom: 10px;
}
.rating, .review {
margin-top: 20px;
text-align: center;
}
.star-rating {
display: flex;
justify-content: center;
}
.star-rating .star {
font-size: 30px;
color: #444;
cursor: pointer;
transition: color 0.2s;
}
.star-rating .star.selected {
color: #f5b301;
}
.review textarea {
width: 80%;
height: 100px;
background-color: #222;
color: #fff;
border: 1px solid #555;
padding: 10px;
}
.review button {
margin-top: 10px;
padding: 10px 20px;
background-color: #f5b301;
border: none;
cursor: pointer;
}
JavaScript Functionality
Create script.js inside public/js/.
// public/js/script.js
document.addEventListener("DOMContentLoaded", () => {
let currentSlide = 0;
let slidesData = [];
const slidesContainer = document.getElementById("slides");
const prevBtn = document.getElementById("prevBtn");
const nextBtn = document.getElementById("nextBtn");
// Fetch images data from the server
fetch("/images")
.then((res) => res.json())
.then((data) => {
slidesData = data;
renderSlides();
});
function renderSlides() {
slidesContainer.innerHTML = "";
slidesData.forEach((slide, index) => {
const slideDiv = document.createElement("div");
slideDiv.classList.add("slide");
if (index === currentSlide) {
slideDiv.classList.add("active");
} else if (index === currentSlide - 1 || (currentSlide === 0 && index === slidesData.length - 1)) {
slideDiv.classList.add("previous");
}
const img = document.createElement("img");
img.src = `/uploads/${slide.filename}`;
img.alt = `Slide ${index + 1}`;
slideDiv.appendChild(img);
// Rating Section
const ratingDiv = document.createElement("div");
ratingDiv.classList.add("rating");
const starRatingDiv = document.createElement("div");
starRatingDiv.classList.add("star-rating");
for (let i = 1; i <= 5; i++) {
const starSpan = document.createElement("span");
starSpan.classList.add("star");
starSpan.innerHTML = "★"; // Star character
if (i <= slide.rating) {
starSpan.classList.add("selected");
}
starSpan.addEventListener("click", () => {
updateRating(index, i);
});
starRatingDiv.appendChild(starSpan);
}
ratingDiv.appendChild(starRatingDiv);
// Review Section
const reviewDiv = document.createElement("div");
reviewDiv.classList.add("review");
const reviewTextarea = document.createElement("textarea");
reviewTextarea.placeholder = "Write a review...";
reviewTextarea.value = slide.review || "";
reviewTextarea.addEventListener("change", () => {
updateReview(index, reviewTextarea.value);
});
reviewDiv.appendChild(reviewTextarea);
slideDiv.appendChild(ratingDiv);
slideDiv.appendChild(reviewDiv);
slidesContainer.appendChild(slideDiv);
});
}
function updateRating(slideIndex, rating) {
slidesData[slideIndex].rating = rating;
fetch(`/rate/${slidesData[slideIndex].id}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ rating }),
});
renderSlides();
}
function updateReview(slideIndex, review) {
slidesData[slideIndex].review = review;
fetch(`/review/${slidesData[slideIndex].id}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ review }),
});
}
// Navigation
prevBtn.addEventListener("click", () => {
currentSlide = (currentSlide - 1 + slidesData.length) % slidesData.length;
renderSlides();
});
nextBtn.addEventListener("click", () => {
currentSlide = (currentSlide + 1) % slidesData.length;
renderSlides();
});
});
Back-End Development
Setting Up the Server
Create app.js in the root of your project.
// app.js
const express = require("express");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files
app.use(express.static(path.join(__dirname, "public")));
app.use("/uploads", express.static(path.join(__dirname, "public/uploads")));
// Storage config for multer
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploads/");
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + path.extname(file.originalname);
cb(null, uniqueSuffix);
},
});
const upload = multer({ storage: storage });
// Load images data
let imagesData = [];
const dataFilePath = path.join(__dirname, "data", "images.json");
if (fs.existsSync(dataFilePath)) {
imagesData = JSON.parse(fs.readFileSync(dataFilePath));
}
// Routes
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views", "index.html"));
});
app.get("/images", (req, res) => {
res.json(imagesData);
});
// Handle image upload
app.post("/upload", upload.single("image"), (req, res) => {
const newImage = {
id: Date.now(),
filename: req.file.filename,
rating: 0,
review: "",
};
imagesData.push(newImage);
saveImagesData();
res.redirect("/");
});
// Handle rating update
app.post("/rate/:id", (req, res) => {
const imageId = parseInt(req.params.id);
const { rating } = req.body;
const image = imagesData.find((img) => img.id === imageId);
if (image) {
image.rating = rating;
saveImagesData();
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
// Handle review update
app.post("/review/:id", (req, res) => {
const imageId = parseInt(req.params.id);
const { review } = req.body;
const image = imagesData.find((img) => img.id === imageId);
if (image) {
image.review = review;
saveImagesData();
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
// Save images data to JSON file
function saveImagesData() {
fs.writeFileSync(dataFilePath, JSON.stringify(imagesData, null, 2));
}
// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Running the Application Locally
-
Ensure the Data Directory Exists
Make sure the data/ directory exists and contains an empty images.json file.
// data/images.json
[]
-
Run the Server
In the terminal, start your Node.js server:
node app.js
-
Open the Application
Open your web browser and navigate to http://localhost:3000.
-
Using Live Server in VS Code
Since you're running a Node.js server, you don't need to use the Live Server extension in VS Code. The Express server is serving your application.
Conclusion
You now have a web application that:
- Allows you to upload images from your computer.
- Displays images in a slideshow with next and previous navigation.
- Shows previous images slightly visible when navigating.
- Enables individual rating of each image with a 5-star rating system.
- Allows you to write and save a review for each image.
- Saves all data locally in a JSON file.
Optional Enhancements
- Database Integration: For more robust data handling, consider integrating a database like SQLite or MongoDB.
- Authentication: Implement user authentication to personalize ratings and reviews.
- Responsive Design: Improve the CSS to make the website responsive on different screen sizes.
- Image Previews: Add thumbnails or small previews of other images in the slideshow.
Troubleshooting Tips
- CORS Errors: Make sure all your requests are being sent to the correct endpoints.
- File Paths: Verify that all file paths are correct, especially when serving static files.
- Dependencies: Ensure all Node.js packages are installed by checking
node_modules folder or reinstalling with npm install.
Feel free to customize and enhance the code to suit your preferences. If you have any questions or need further assistance, don't hesitate to ask!