如何在 Nodejs Replit 中播放 mp3 文件?

How can I play a mp3 file in a Nodejs Replit?

我有一个 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

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

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

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