-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
126 lines (110 loc) · 3.04 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
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
const next = require('next');
const express = require('express');
const path = require('path');
const fs = require('fs');
const { trimString } = require('./utils');
const bestNewDb = require('./data/best-new.json');
const top10Db = require('./data/top10.json');
const getTags = require('./data/getTags');
const getArtists = require('./data/getArtists');
const getBestNew = require('./data/getBestNew');
// create next app
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use('/static', express.static('static'));
server.get('/api/tag/:name', (req, res) => {
const name = trimString(req.params.name);
let data;
try {
data = getTags(name, req.query);
} catch (err) {
return res.redirect('/404');
}
res.json(data);
});
server.get('/api/artist/:name', (req, res) => {
const name = trimString(req.params.name);
let data;
try {
data = getArtists(name, req.query);
} catch (err) {
return res.redirect('/404');
}
res.json(data);
});
server.get('/api/best-new', (req, res) => {
const data = getBestNew(req.query);
res.json(data);
});
server.get('/', (req, res) => {
const tags = getTags('all', {});
const artists = getArtists('all', {});
const data = {
tags: tags.data,
artists: artists.data
};
return app.render(req, res, '/', data);
});
server.get('/best-new', (req, res) => {
const artists = getBestNew(req.query);
const artistNames = getArtists('all', {});
const data = {
artists: artists.data,
artistNames: artistNames.data
};
return app.render(req, res, '/best-new', data);
});
server.get(['/artist', ['/tag']], (req, res) => {
return res.redirect('/404');
});
server.get('/artist/:name', (req, res) => {
let name = req.params.name;
let artists;
try {
artists = getArtists(name, { similar: 'true' });
} catch (err) {
return res.redirect('/404');
}
const data = artists.data;
return app.render(req, res, '/artist', data);
});
server.get('/tag/:name', (req, res) => {
let name = req.params.name;
let artists;
try {
artists = getTags(name, { random: 'true' });
} catch (err) {
return res.redirect('/404');
}
const data = artists.data;
return app.render(req, res, '/tag', data);
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(8080, err => {
if (err) throw err;
console.log('> Ready on 8080');
});
});
// // response API for top-10 request
// server.get('/api/top-10/:name', function (req, res) {
// let name = trimString(req.params.name);
// let data = { data: [] };
// top10Db.forEach(item => {
// if (name === 'all') {
// data.data.push(item.title);
// } else if (trimString(item.title) === name) {
// data.data.push(item);
// }
// });
// if (data.data.length <= 0) res.json({ data: "error 404", message: "top 10 couldn't be found" });
// res.json(data);
// });
// // send index.html for every page
// server.get('/*', function (req, res) {
// res.sendFile(indexPath);
// });