Certainly! To create HTML files that interact with your API endpoints, you can develop simple front-end pages using HTML, JavaScript, and possibly some CSS for styling. These pages would typically use JavaScript to make AJAX requests to your API endpoints.
Here's a structured approach to help you get started with creating HTML pages that interact with your API:
1. Project Structure
You could have a basic project structure like this:
project-root/
│
├── public/
│ ├── index.html
│ ├── js/
│ │ ├── main.js
│ └── css/
│ └── styles.css
├── server.js
└── [other folders and files related to your server-side code]
2. HTML Setup (index.html)
Create a simple HTML page to serve as your front end:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video Hosting Platform</title>
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
<h1>Welcome to the Video Hosting Platform</h1>
<form id="uploadForm">
<input type="file" id="videoFile" name="videoFile" accept="video/*" required>
<button type="submit">Upload Video</button>
</form>
<script src="/js/main.js"></script>
</body>
</html>
3. JavaScript (main.js)
Add JavaScript to handle form submissions and interact with your API:
document.addEventListener('DOMContentLoaded', () => {
const uploadForm = document.getElementById('uploadForm');
uploadForm.addEventListener('submit', async (e) => {
e.preventDefault();
const videoFile = document.getElementById('videoFile').files[0];
if (!videoFile) {
alert('Please select a video file to upload.');
return;
}
const formData = new FormData();
formData.append('video', videoFile);
try {
const response = await fetch('/api/videos', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error('Failed to upload video');
}
const result = await response.json();
alert('Video uploaded successfully: ' + result.videoUrl);
} catch (error) {
console.error('Error uploading video:', error);
alert('Error uploading video');
}
});
});
4. Style with CSS (styles.css)
Add some basic styling to make your front end look better:
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
}
form {
margin-top: 20px;
}
5. Serve Public Files
In your server.js, serve the public directory to allow access to your HTML, JavaScript, and CSS files:
// Serve static files (HTML, CSS, JS)
app.use(express.static(path.join(__dirname, 'public')));
6. Run the Application
Make sure your server is running, and then you should be able to access your HTML page by going to http://localhost:5000 (or whichever port your server is running on).
Additional Features
This example only covers a basic video upload. You can expand this by adding more features such as user authentication, video listing, comments section, etc., by creating additional HTML pages and JavaScript logic to handle other aspects of your API.
Feel free to ask if you need help with specific features or details!