Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)
- mkdir music-player
- cd music-player
- 2)npm init -y
- 3)npm install express multer ejs express-session
- 4)- music-player/
- - uploads/ (папка для сохранения загруженных MP3 файлов)
- - views/ (шаблоны EJS)
- - index.ejs
- - upload.ejs
- - player.ejs
- - public/ (статические файлы: CSS, изображения)
- - styles.css
- - app.js (основной файл приложения)
- 5)// app.js
- const express = require('express');
- const multer = require('multer');
- const session = require('express-session');
- const path = require('path');
- const fs = require('fs');
- const app = express();
- // Настройка сессии
- app.use(
- session({
- secret: 'your_secret_key',
- resave: false,
- saveUninitialized: true,
- })
- );
- // Установка шаблонизатора EJS
- app.set('view engine', 'ejs');
- // Папка для статических файлов
- app.use(express.static('public'));
- // Настройка хранения файлов с помощью Multer
- const storage = multer.diskStorage({
- destination: function (req, file, cb) {
- const userDir = path.join(__dirname, 'uploads', req.sessionID);
- if (!fs.existsSync(userDir)) {
- fs.mkdirSync(userDir, { recursive: true });
- }
- cb(null, userDir);
- },
- filename: function (req, file, cb) {
- cb(null, file.originalname);
- },
- });
- const upload = multer({ storage: storage });
- // Главная страница
- app.get('/', (req, res) => {
- res.render('index');
- });
- // Страница загрузки файлов
- app.get('/upload', (req, res) => {
- res.render('upload');
- });
- // Обработка загрузки файла
- app.post('/upload', upload.single('mp3file'), (req, res) => {
- res.redirect('/music');
- });
- // Страница со списком загруженных треков
- app.get('/music', (req, res) => {
- const userDir = path.join(__dirname, 'uploads', req.sessionID);
- let files = [];
- if (fs.existsSync(userDir)) {
- files = fs.readdirSync(userDir);
- }
- res.render('player', { files: files });
- });
- // Маршрут для прослушивания трека
- app.get('/music/:filename', (req, res) => {
- const filePath = path.join(
- __dirname,
- 'uploads',
- req.sessionID,
- req.params.filename
- );
- res.sendFile(filePath);
- });
- // Запуск сервера
- const PORT = 3000;
- app.listen(PORT, () => {
- console.log(`Сервер запущен на http://localhost:${PORT}`);
- });
- 6)<!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <title>Музыкальный Плеер</title>
- <link rel="stylesheet" href="/styles.css">
- </head>
- <body>
- <h1>Добро пожаловать в Музыкальный Плеер</h1>
- <a href="/upload">Загрузить музыку</a>
- <a href="/music">Моя музыка</a>
- </body>
- </html>
- 7)<!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <title>Загрузка Музыки</title>
- <link rel="stylesheet" href="/styles.css">
- </head>
- <body>
- <h1>Загрузить MP3 файл</h1>
- <form action="/upload" method="post" enctype="multipart/form-data">
- <input type="file" name="mp3file" accept=".mp3" required>
- <button type="submit">Загрузить</button>
- </form>
- <a href="/">На главную</a>
- </body>
- </html>
- 8)<!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <title>Загрузка Музыки</title>
- <link rel="stylesheet" href="/styles.css">
- </head>
- <body>
- <h1>Загрузить MP3 файл</h1>
- <form action="/upload" method="post" enctype="multipart/form-data">
- <input type="file" name="mp3file" accept=".mp3" required>
- <button type="submit">Загрузить</button>
- </form>
- <a href="/">На главную</a>
- </body>
- </html>
- 9)body {
- font-family: Arial, sans-serif;
- margin: 20px;
- }
- h1 {
- color: #333;
- }
- a {
- margin-right: 10px;
- }
- ul {
- list-style-type: none;
- }
- li {
- margin-bottom: 20px;
- }
- 10)node app.js
- 11)Перейди в браузере по адресу http://localhost:3000 и протестируй приложение.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement