app.get('/download/:id', (req, res) => { const id = req.params.id; Song.findById(id, (err, song) => { if (err) { res.status(404).send(err); } else { const file = cloudStorage.getFile(song.url); res.set("Content-Disposition", `attachment; filename="${song.title}.mp3"`); res.set("Content-Type", "audio/mpeg"); file.pipe(res); } }); });
useEffect(() => { axios.get(`http://localhost:3000/search?q=${searchQuery}`) .then(response => { setSongs(response.data); }) .catch(error => { console.error(error); }); }, [searchQuery]);
return ( <div> <input type="search" value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} placeholder="Search for songs" /> <ul> {songs.map((song) => ( <li key={song._id}> {song.title} by {song.artist} <button onClick={() => handleDownload(song)}>Download</button> </li> ))} </ul> {currentSong && ( <audio controls> <source src={URL.createObjectURL(currentSong)} type="audio/mpeg" /> Your browser does not support the audio element. </audio> )} </div> ); }
const Song = mongoose.model('Song', songSchema);