Advertisement
Guest User

Untitled

a guest
Sep 13th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. 1)
  2. mkdir music-player
  3. cd music-player
  4. 2)npm init -y
  5. 3)npm install express multer ejs express-session
  6. 4)- music-player/
  7. - uploads/ (папка для сохранения загруженных MP3 файлов)
  8. - views/ (шаблоны EJS)
  9. - index.ejs
  10. - upload.ejs
  11. - player.ejs
  12. - public/ (статические файлы: CSS, изображения)
  13. - styles.css
  14. - app.js (основной файл приложения)
  15. 5)// app.js
  16. const express = require('express');
  17. const multer = require('multer');
  18. const session = require('express-session');
  19. const path = require('path');
  20. const fs = require('fs');
  21.  
  22. const app = express();
  23.  
  24. // Настройка сессии
  25. app.use(
  26. session({
  27. secret: 'your_secret_key',
  28. resave: false,
  29. saveUninitialized: true,
  30. })
  31. );
  32.  
  33. // Установка шаблонизатора EJS
  34. app.set('view engine', 'ejs');
  35.  
  36. // Папка для статических файлов
  37. app.use(express.static('public'));
  38.  
  39. // Настройка хранения файлов с помощью Multer
  40. const storage = multer.diskStorage({
  41. destination: function (req, file, cb) {
  42. const userDir = path.join(__dirname, 'uploads', req.sessionID);
  43. if (!fs.existsSync(userDir)) {
  44. fs.mkdirSync(userDir, { recursive: true });
  45. }
  46. cb(null, userDir);
  47. },
  48. filename: function (req, file, cb) {
  49. cb(null, file.originalname);
  50. },
  51. });
  52.  
  53. const upload = multer({ storage: storage });
  54.  
  55. // Главная страница
  56. app.get('/', (req, res) => {
  57. res.render('index');
  58. });
  59.  
  60. // Страница загрузки файлов
  61. app.get('/upload', (req, res) => {
  62. res.render('upload');
  63. });
  64.  
  65. // Обработка загрузки файла
  66. app.post('/upload', upload.single('mp3file'), (req, res) => {
  67. res.redirect('/music');
  68. });
  69.  
  70. // Страница со списком загруженных треков
  71. app.get('/music', (req, res) => {
  72. const userDir = path.join(__dirname, 'uploads', req.sessionID);
  73. let files = [];
  74. if (fs.existsSync(userDir)) {
  75. files = fs.readdirSync(userDir);
  76. }
  77. res.render('player', { files: files });
  78. });
  79.  
  80. // Маршрут для прослушивания трека
  81. app.get('/music/:filename', (req, res) => {
  82. const filePath = path.join(
  83. __dirname,
  84. 'uploads',
  85. req.sessionID,
  86. req.params.filename
  87. );
  88. res.sendFile(filePath);
  89. });
  90.  
  91. // Запуск сервера
  92. const PORT = 3000;
  93. app.listen(PORT, () => {
  94. console.log(`Сервер запущен на http://localhost:${PORT}`);
  95. });
  96. 6)<!DOCTYPE html>
  97. <html lang="ru">
  98. <head>
  99. <meta charset="UTF-8">
  100. <title>Музыкальный Плеер</title>
  101. <link rel="stylesheet" href="/styles.css">
  102. </head>
  103. <body>
  104. <h1>Добро пожаловать в Музыкальный Плеер</h1>
  105. <a href="/upload">Загрузить музыку</a>
  106. <a href="/music">Моя музыка</a>
  107. </body>
  108. </html>
  109.  
  110. 7)<!DOCTYPE html>
  111. <html lang="ru">
  112. <head>
  113. <meta charset="UTF-8">
  114. <title>Загрузка Музыки</title>
  115. <link rel="stylesheet" href="/styles.css">
  116. </head>
  117. <body>
  118. <h1>Загрузить MP3 файл</h1>
  119. <form action="/upload" method="post" enctype="multipart/form-data">
  120. <input type="file" name="mp3file" accept=".mp3" required>
  121. <button type="submit">Загрузить</button>
  122. </form>
  123. <a href="/">На главную</a>
  124. </body>
  125. </html>
  126.  
  127. 8)<!DOCTYPE html>
  128. <html lang="ru">
  129. <head>
  130. <meta charset="UTF-8">
  131. <title>Загрузка Музыки</title>
  132. <link rel="stylesheet" href="/styles.css">
  133. </head>
  134. <body>
  135. <h1>Загрузить MP3 файл</h1>
  136. <form action="/upload" method="post" enctype="multipart/form-data">
  137. <input type="file" name="mp3file" accept=".mp3" required>
  138. <button type="submit">Загрузить</button>
  139. </form>
  140. <a href="/">На главную</a>
  141. </body>
  142. </html>
  143. 9)body {
  144. font-family: Arial, sans-serif;
  145. margin: 20px;
  146. }
  147.  
  148. h1 {
  149. color: #333;
  150. }
  151.  
  152. a {
  153. margin-right: 10px;
  154. }
  155.  
  156. ul {
  157. list-style-type: none;
  158. }
  159.  
  160. li {
  161. margin-bottom: 20px;
  162. }
  163. 10)node app.js
  164. 11)Перейди в браузере по адресу http://localhost:3000 и протестируй приложение.
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement