-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
75 lines (60 loc) · 2.36 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
const express = require('express');
const path = require('path');
const app = express();
const github = require('./server/github');
var bodyParser = require('body-parser');
app.use(express.static('./build'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(function(req, res, next) {
var origin = req.get('origin');
if (process.env.NODE_ENV === "development") {
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Origin", process.env.WEBSITES_CM_ACCEPT || "http://localhost:3000");
res.header("Access-Control-Allow-Credentials", true);
}
next();
});
app.post('/api/test', function(req, res) {
var body = req.body;
body.username = req.headers['jwt-un'];
res.status(200).send(body);
});
app.get('/api/all', (req, res) => Promise.resolve(
(req.headers && req.headers['jwt-un']) ? JSON.parse(req.headers['jwt-un']) : 'Unknown')
.then(username => {
var labels = req.query && req.query.labels ? req.query.labels : null;
return { username: username, labels: labels};
})
.then(result => github.getSuggestionsAndVoteTotals(result.username, result.labels))
.then(res.send.bind(res))
.catch(error => res.status(200).json({ error })));
app.post('/api/vote', (req, res) => Promise.resolve(
(req.headers && req.headers['jwt-un']) ? JSON.parse(req.headers['jwt-un']) : 'Unknown')
.then(username => Object.assign(req.body, { username }))
.then(mergedBody => Promise.all([
github.voteForSuggestions(mergedBody),
res.send.bind(res)
]))
.catch(error => res.status(503).json({ error })));
app.post('/api/create', (req, res) => Promise.resolve (
(req.headers && req.headers['jwt-un']) ? JSON.parse(req.headers['jwt-un']) : 'Unknown')
.then(username => Object.assign(req.body, { username }))
.then(mergedBody => github.createSuggestion(mergedBody))
.then(res.send.bind(res))
.catch(error => res.status(503).json({ error })));
app.get('/api/labels', (req, res) => {
return github.getLabels().then(res.send.bind(res));
});
app.get('/api/labels/tags', (req, res) => {
return github.getTagLabels().then(res.send.bind(res));
});
app.get('/api/labels/categories', (req, res) => {
return github.getCategoryLabels().then(res.send.bind(res));
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'));
});
app.listen(9000);