-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (35 loc) · 1.29 KB
/
server.js
File metadata and controls
42 lines (35 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const port = 3000;
const mediaFolderPath = './views'; // Replace with the path to your media folder
// Set up multer for handling file uploads
const storage = multer.diskStorage({
destination: mediaFolderPath,
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname));
},
});
const upload = multer({ storage: storage, limits: { fileSize: 10 * 1024 * 1024 } });
// Serve the HTML page with the file upload form
app.get('/', (req, res) => {
const uploadForm = `
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="mp3File" accept=".mp3" required>
<button type="submit">Upload MP3</button>
</form>
`;
// Send the HTML with the file upload form to the client
res.send(uploadForm);
});
// Endpoint for handling file uploads
app.post('/upload', upload.single('mp3File'), (req, res) => {
// File has been uploaded successfully
res.send('File uploaded successfully!');
});
// Start the server
app.listen(port, () => {
console.log('Server is running on port http://localhost:' + port);
});