forked from MusicStrike/musicApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (80 loc) · 2.46 KB
/
server.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const request = require('request');
let Music_API_KEY = process.env.Music_API_KEY;
const superagent = require('superagent');
const PORT = process.env.PORT || 8080;
const pg = require('pg')
let app = express();
// app.use(express.static('public'))
//app.use('/static', express.static('public'))
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'))
app.get('/test', (req,res) =>{
// var request = require("request");
// let reqJson = req.body
// res.render('music/show')
var options = {
method: 'GET',
url: `https://deezerdevs-deezer.p.rapidapi.com/search?q=${req.query.input}`,
qs: {q: req.query.input },
headers: {
'x-rapidapi-host': 'deezerdevs-deezer.p.rapidapi.com',
'x-rapidapi-key': Music_API_KEY
}
}
// superagent(options.url)
// .then(musicData =>{
// let jSonData =musicData.body.data
// console.log(jSonData)
// console.log('hi');
// })
request(options, function (error, response, body) {
// console.log(options.qs.q)
if (error) throw new Error(error)
// superagent.get(options.url)
//let jSonData =body.data
//console.log(jSonData)
let jSonData = JSON.parse(body)
let song = jSonData.data.map(singer => new Music(singer))
// res.redirect('/')
res.render('pages/new' , {list:song});
});
// for(let i=0 ;i<jSonData.data.length; i++){
// res.send(jSonData.data[i].title)
// }
// let rendered = jSonData.data.forEach(song => {
// let formatted = new Music(song)
// console.log(formatted)
// })
// let hello = jSonData.forEach(song => {
// let formatted = new Music(song)
// console.log(formatted)
// })
//res.render(`music/show` , hello)
function Music(singer){
this.title=singer.title
this.preview_mp3=singer.preview
this.artistName=singer.artist.name
this.album_cover_image=singer.album.cover_medium
this.album_title=singer.album.title
}
})
app.get('/', (req,res) => {
// console.log(req);
res.render('pages/index');
})
app.get('*', (req, res) => {
// console.log('this is for checking ', req);
res.status(404).render('./pages/error', { erorr: '404 NOT FOUND' })
});
const client = new pg.Client(process.env.DATABASE_URL);
client.connect(console.log('im DB'))
.then( () => {
app.listen(PORT, () => {
console.log(`Host : ${PORT}` )
})
})