forked from TrendGame/TrendGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (62 loc) · 1.55 KB
/
server.js
File metadata and controls
74 lines (62 loc) · 1.55 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
const makeTimeline = require('./utilities/makeTimeline');
const queries = require('./db/queries');
const cleanData = require('./utilities/cleanSearch');
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();
const IP = '127.0.0.1';
const PORT = process.env.PORT || 8080;
app.use(express.static(__dirname + '/client/public'));
app.use(bodyParser.json());
app.use(morgan('tiny'));
if (!module.parent) {
app.listen(PORT, () => {
console.log(`Listening on ${IP}:${PORT}`);
});
}
app.get('/api', (req, res) => {
res.send({
version: '0.0.1'
});
});
app.get('/api/timeline', (req, res) => {
let trend = req.query.q;
trend = cleanData.prepForAylien(trend);
makeTimeline(trend, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(data);
}
});
});
app.post('/api/history', (req, res) => {
let trend = req.body.search;
if (cleanData.checkIsReadyForDb(trend)) {
trend = cleanData.prepForDb(trend);
queries.insertSearch(trend, (err, resp) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(resp);
}
});
} else {
res.status(400).send();
}
});
app.get('/api/history', (req, res) => {
queries.getSearches(10, (err, data) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send(data);
}
});
});
app.use((req, res) => {
res.status(404);
res.sendFile(__dirname + '/404.html');
});
module.exports = app;