scnario commited on
Commit
c4b7c29
·
verified ·
1 Parent(s): f34fe50

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -9
server.js CHANGED
@@ -11,19 +11,27 @@ const stringify = (value) => {
11
  }
12
 
13
  app.all('/play', async(req, res) => {
14
- const { q } = req.query || req.body
15
- if(!q) return res.json({ status: false, error: '"q" parameter is undefined!'})
 
16
 
17
- const search = await Spotify.search(q, "track", 1)
18
- //if(!Array.isArray(search)) return res.json({ status: false, error: 'Result is not array.'})
19
- res.json({ status: true, result: Array.isArray(search) ? search[0] : search, download: `https://${req.hostname}/track/${search[0]?.id}` });
 
 
 
20
  })
21
 
22
  app.all('/track/:id', async(req, res) => {
23
- const { id } = req.params || res.params
24
- if(!id || !id.length > 21) return res.json({ status: false, error: "Track ID is not valid." })
25
- const spotify = await Spotify.download(`https://open.spotify.com/track/${id}`)
26
- res.redirect(spotify?.download)
 
 
 
 
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)