-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (44 loc) · 1.3 KB
/
app.js
File metadata and controls
50 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var express = require('express'),
app = express(),
fetch = require('node-fetch'),
port = process.env.PORT;
app.set("view engine", "ejs");
app.use("/public", express.static(__dirname +'/public'));
app.use(getTracksMiddleware);
app.get('/', (req, res)=>{
var url="https://api.deezer.com/playlist/1313621735/?index=0&limit=3&output=json";
getTracks(url,"home","error")
})
app.get('/search', (req, res)=>{
var term= req.query.q,
index=req.query.index;
if(!term){
res.redirect("/")
}if(!index){
index=0
}
var url="https://api.deezer.com/search?q="+term+"&limit=12&output=json"+"&index="+index;
getTracks(url,"search","error");
})
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function(){
console.log("\x1b[32m%s\x1b[0m", "songArts is running on PORT: "+port)
})
function getTracksMiddleware(req, res, next){
getTracks = function(url, successPath, failPath){
fetch(url)
.then(function(resp){
if(resp.ok){
return resp.json()
}
}).then((data)=>{
var tracks=data
res.render(successPath,{tracks: tracks, term: req.query.q})
}).catch((err)=>{
res.render(failPath, {err: err})
})
}
next();
}