【问题标题】:How can I play a mp3 file in a Nodejs Replit?如何在 Nodejs Replit 中播放 mp3 文件?
【发布时间】:2021-05-17 18:08:36
【问题描述】:

我有一个 mp3 文件,我想在按下按钮时播放它。但我相信我没有正确地将文件提供给服务器。这是 Replit.com 上的代码:

const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env['port'];
const admin = require('firebase-admin');
const express = require("express");
const path = require("path");

admin.initializeApp();

const db = admin.firestore();

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index/index.html');
});
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index/script.js');
});
app.get('/', (req, res) => {
    res.sendFile(__dirname + './index/ding.mp3');
});

io.on('connection', (socket) => {
    socket.on('chat message', (msg) => {
        if (msg === "ping") {
            io.emit('chat message', "pong");
        }
        io.emit('chat message', msg);
    });
    socket.on('serverLog', (m) => {
        console.log(m);
    });
    socket.on('loggedInWithName', (name) => {
        console.log('User Connected. Username: ' + name);
    });

});

http.listen(port, () => {
    console.log(`Socket.IO server running at http://localhost:${port}`);
});

Replit.com

【问题讨论】:

    标签: javascript express socket.io repl.it


    【解决方案1】:

    我已经测试了 repl,它可以工作。检查您的音量设置。

    提示。如果您不想为每个静态文件重复 res.sendFile,我建议使用此表示法:

    app.use('/static', express.static(path.join(__dirname, 'public')))
    

    【讨论】:

    • 是的!谢谢!
    猜你喜欢
    • 2012-04-11
    • 2013-05-28
    • 2010-09-30
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多