Update server.js
Browse files
server.js
CHANGED
|
@@ -11,19 +11,27 @@ const stringify = (value) => {
|
|
| 11 |
}
|
| 12 |
|
| 13 |
app.all('/play', async(req, res) => {
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
})
|
| 21 |
|
| 22 |
app.all('/track/:id', async(req, res) => {
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
})
|
| 28 |
|
| 29 |
app.listen(PORT)
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
app.all('/play', async(req, res) => {
|
| 14 |
+
try {
|
| 15 |
+
const { q } = req.query || req.body
|
| 16 |
+
if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
|
| 17 |
|
| 18 |
+
const search = await Spotify.search(q, "track", 1)
|
| 19 |
+
//if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'})
|
| 20 |
+
res.json({ status: true, result: Array.isArray(search) ? search[0] : search, download: `https://${req.hostname}/track/${search[0]?.id}` });
|
| 21 |
+
} catch(e) {
|
| 22 |
+
res.json({ status: false, error: e || e.message })
|
| 23 |
+
}
|
| 24 |
})
|
| 25 |
|
| 26 |
app.all('/track/:id', async(req, res) => {
|
| 27 |
+
try {
|
| 28 |
+
const { id } = req.params || res.params
|
| 29 |
+
if(!id || !id.length > 21) return res.json({ status: false, error: "Track ID is not valid." })
|
| 30 |
+
const spotify = await Spotify.download(`https://open.spotify.com/track/${id}`)
|
| 31 |
+
res.redirect(spotify?.download)
|
| 32 |
+
} catch(e) {
|
| 33 |
+
res.json({ status: false, error: e || e.message })
|
| 34 |
+
}
|
| 35 |
})
|
| 36 |
|
| 37 |
app.listen(PORT)
|