-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (64 loc) · 2.12 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
import express from 'express';
import request from 'request';
const app = express();
// Serve application file depending on environment
app.get('/app.js', (req, res) => {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/build/app.js');
} else {
res.redirect('//localhost:9090/build/app.js');
}
});
app.get('/api/artists', (req, res) => {
var url = "http://api-3283.iheart.com/api/v1/catalog/searchAll?keywords=";
var queryOptions = "&queryTrack=false&queryBundle=false&queryArtist=true&queryStation=false&queryFeaturedStation=false&queryTalkShow=false&queryTalkTheme=false&queryKeyword=false&countryCode=US";
var query = url + req.query.title + queryOptions;
var options = { uri: query, 'Content-Type': 'application/json;charset=UTF-8'};
request.get(options, (error, response, body) => {
// DO: Add Error handling
var body = JSON.parse(body);
var artists = body.artists;
if (Array.isArray(artists)) {
artists = artists.slice(0,6);
}
res.send({artists: artists});
})
})
// Serve aggregate stylesheet depending on environment
app.get('/style.css', (req, res) => {
if (process.env.PRODUCTION) {
res.sendFile(__dirname + '/build/style.css');
} else {
res.redirect('//localhost:9090/build/style.css');
}
});
// Serve index page
app.get('*', (req, res) => {
res.sendFile(__dirname + '/src/index.html');
});
if (!process.env.PRODUCTION) {
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.local.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
noInfo: true,
historyApiFallback: true
}).listen(9090, 'localhost', (err, result) => {
if (err) {
console.log(err);
}
});
}
/******************
*
* Express server
*
*****************/
const port = process.env.PORT || 8080;
const server = app.listen(port, () => {
const host = server.address().address;
const port = server.address().port;
console.log('Listening at:', port);
});