-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (113 loc) · 3.92 KB
/
server.js
File metadata and controls
125 lines (113 loc) · 3.92 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require('babel-core/register')({
presets: ['es2015', 'react']
})
var request = require('request');
var express = require('express')
, app = express()
, React = require('react')
, ReactDOMServer = require('react-dom/server')
, components = require('./app/Main.js')
/* --INITIAL SETUP-- */
//default info, Please use your own API key!!
var apiKey = '&key=AIzaSyCxrc0vDQf7b6IuLxph310QZFsh8tSnpc0',
apiURL = 'https://www.googleapis.com/youtube/v3/search?';
var apiDefaultParams = 'part=snippet&maxResults=5&type=video&fields=items(id(videoId)%2Csnippet(title%2C+thumbnails(medium(url))))&q='
//my selected artists
var options = ['The Album Leaf', 'Deerhunter', 'Jason Mraz' ,'Boards of Canada', 'Mogwai', 'React Europe'];
//create a map for the use for query
var queryMap = {};
//this is used to change the options into query style used by the Youtube API
var getQueryWord = function(str) {
var words = str.split(' ');
var queryWord = '';
words.map(function(word, i) {
if (i != 0) {
queryWord += '+' + word.toLowerCase(word);
} else {
queryWord += word.toLowerCase(word);
}
});
return queryWord;
};
options.map(function(option) {
queryMap[option] = getQueryWord(option);
});
//function to return a variable API url
var getURL = function(option) {
return apiURL + apiDefaultParams + queryMap[option] + apiKey;
}
//parse the result get from the API to the proper form used by our React element
var parseResult = function(result) {
var retArray = [];
result.map(function(item) {
var videoId = item.id.videoId;
var videoTitle = item.snippet.title;
var imgURL = item.snippet.thumbnails.medium.url;
retArray.push({
key: videoId,
data: {
videoTitle: videoTitle,
imgURL: imgURL
}
})
});
return retArray;
}
//setup the template engine
app.engine('jade', require('jade').__express)
app.set('view engine', 'jade')
app.use(express.static(__dirname + '/public'))
/* -- Server Side Rendering-- */
//server side load the initial data for the app
request(getURL(options[0]), function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("success call");
var initData = parseResult(JSON.parse(body).items);
// console.log(JSON.parse(body).items);
var initProps = {
options: options,
selected: options[0],
listData: initData
}
// create the React element
var MyApp = React.createFactory(components.MyApp)
// set the root rounter for the app
app.get('/', function(req, res){
res.render('index', {
//Render the React element to HTML mark up
react: ReactDOMServer.renderToString(MyApp({initProps: initProps})),
//serialise the props to JSON so they can be included as a variable in the initial HTML sent to the client:
props: JSON.stringify(initProps)
})
})
} else {
console.log("fail call")
}
});
/*
Important:
React will check that any initial markup present matches what's produced for the first render on the client
by comparing checksums between the initial client render and a checksum attribute in the server-rendered markup,
so I must make the same props available for the initial render on the client in order to reuse the markup.
*/
/* --RESTful API -- */
//set up RESTful api for different options call
options.map(function(option, index) {
app.get(encodeURI('/'+option), function(req, res){
//for each HTTP request from the client, make a http call to the Youtube API to fetch the most update data
//Normally, this should be linked to the database, here is just for the demo purpose
request(getURL(option), function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("success call");
var myResult = parseResult(JSON.parse(body).items);
res.send(JSON.stringify(myResult));
} else {
console.log("fail call")
}
});
});
});
/* --Listen to 3000 --*/
app.listen(3000, function() {
console.log('Listening on port 3000...')
})