-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (64 loc) · 2.33 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
'use strict';
var Promise = require('promise'),
express = require('express'),
pug = require('pug'),
rp = require('request-promise'),
formatListForVue = require('./src/formatListForVue'),
getLyricsUrl = require('./src/getLyricsUrl'),
getUgSearchUrl = require('./src/getUgSearchUrl'),
getGeniusSearchUrl = require('./src/getGeniusSearchUrl'),
parseChordsContent = require('./src/parseChordsContent'),
parseChordsList = require('./src/parseChordsList'),
parseLyricsContent = require('./src/parseLyricsContent'),
reduceToBestChords = require('./src/reduceToBestChords'),
songs = require('./songs'),
app = express();
// Serves static files
app.use(express.static('public'));
// Serves HTML
app.get('/', function(req, res) {
res.end(pug.renderFile('lyrics.html.pug', { songs: formatListForVue(songs) }));
});
// UG API
app.get('/ug(\.html)?', function(req, res) {
var toHtml = req.path.match(/\.html$/);
res.append('Content-Type', 'text/' + toHtml ? 'html' : 'plain');
console.log("UG Search Url : \n > " + getUgSearchUrl(req.query.q))
rp(getUgSearchUrl(req.query.q))
.then(parseChordsList)
.then(reduceToBestChords)
.then(rp)
.then(parseChordsContent.bind(null, toHtml))
.then(res.end.bind(res))
.catch(function(error) {
var statusCode = error.statusCode || 500;
res.status(statusCode);
if (statusCode === 404) {
res.end('No chords found for "' + req.query.q + '"');
} else {
console.error(error);
res.end();
}
});
});
// Genius API
app.get('/genius(\.html)?', function(req, res) {
var toHtml = req.path.match(/\.html$/);
res.append('Content-Type', 'text/' + toHtml ? 'html' : 'plain');
rp(getGeniusSearchUrl(req.query.q))
.then(getLyricsUrl)
.then((url) => rp(url))
.then(parseLyricsContent.bind(null, toHtml))
.then(res.end.bind(res))
.catch(function(error) {
var statusCode = error.statusCode || 500;
res.status(statusCode);
if (statusCode === 404) {
res.end('No lyrics found for "' + req.query.q + '"');
} else {
console.error(error);
res.end();
}
});
});
app.listen(3000);