-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (59 loc) · 1.66 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
const dotenv = require('dotenv');
dotenv.config();
const express = require('express');
const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const firebase = require('firebase');
const app = express();
//DbConfig
const config = {
apiKey: process.env.APIKEY,
authDomain: process.env.AUTHDOMAIN,
databaseURL: process.env.TESTURL,
projectId: process.env.PROJECTID,
storageBucket: process.env.PROJECTBUCKET,
messagingSenderId: process.env.ID
};
firebase.initializeApp(config);
const db = firebase.firestore();
//Paths
const pathToBuildFolder = path.join(__dirname, 'build');
const pathToIndexHtmlInBuildFolder = path.join(pathToBuildFolder, 'index.html');
app.use('/', express.static(pathToBuildFolder));
app.use(helmet());
app.use(cors());
app.use(express.json());
//Routes
if (process.env.NODE_ENV === 'production') {
app.get('/', (req, res) => {
res.sendFile(pathToIndexHtmlInBuildFolder);
});
app.get('/about', (req, res) => {
res.sendFile(pathToIndexHtmlInBuildFolder);
});
app.get('/:hacker', (req, res) => {
res.sendFile(pathToIndexHtmlInBuildFolder);
});
}
app.get('/api/hacker/:hacker', async function(req, res) {
try {
const hackers = await db
.collection('commentor_stats')
.where('commentor', '==', req.params.hacker)
.get();
if (hackers.empty) {
console.log('No matching hacker');
return;
}
let hacker;
hackers.forEach(doc => {
hacker = doc.data();
});
res.status(200).json(hacker);
} catch (error) {
res.status(500).json({ message: error.message });
}
console.log('Get Request');
});
app.listen(process.env.PORT || 2222);