-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
135 lines (101 loc) · 3.2 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
126
127
128
129
130
131
132
const express = require('express');
const authRoutes = require('./routes/auth-routes');
const passportSetup = require('./config/passport-setup');
const path = require('path');
const cors = require('cors');
const pg = require('pg');
const passport = require('passport');
const cookieSession = require('cookie-session');
const keys = require('./config/keys');
const app = express();
const cookieParser = require('cookie-parser');
app.use('/build',express.static(path.join(__dirname, 'build')));
app.use(cookieSession({
maxAge: 24*60*60*1000,
keys: [keys.session.cookieKey]
}));
app.use(passport.initialize());
app.use(passport.session());
let user = 'ulurpczi';
let pass = 'TVQxxaVGcvh2ZFlNZHXaHReKN_3DfZbm';
let config = {
host:"nutty-custard-apple.db.elephantsql.com",
user: user,
password: pass,
database: user,
post: 5432,
ssl: true
}
//Generating pool API
let pool = new pg.Pool(config);
let db;
pool.connect((err, result) => {
if (err) throw new Error(err);
else console.log("Connecting to DB...");
db = result;
//old query(just for reference)
// db.query('SELECT * FROM snackify;', (err, result) => {
// if(err){
// throw new Error(err)
// }
// console.log('------>Looking for rows', result.rows);
// db.end();
// });
app.use('/auth', authRoutes);
////////////////
//////home/////
///////////////
app.get('/', (req, res) => {
console.log(req.user, 'from the home route');
res.sendFile(path.join(__dirname, 'index.html'));
});
//=================================================================
app.get('/currentuser', (req, res) =>{
//make request to DB and for req.user value and send to client
res.send('example Data');
});
//=================================================================
app.get('/test', (req, res) => {
console.log(req.session, 'req.session from test');
console.log(req.user, 'im from the /test');
res.send(req.user);
// res.sendFile(path.join(__dirname, 'index.html'));
// res.send(`this is my body ${req.user} `);
});
app.post('/submission',(req,res)=>{
db.query(`UPDATE snackify SET submissionCount = submissionCount -1 WHERE '${req.user.userName}';
UPDATE snackify SET snackphoto = '${req.user.snackphoto} WHERE '${req.user.userName}';
UPDATE snackify SET comments = '${req.user.comments}' WHERE '${req.user.userName}';`,
(err,result)=>{
if(err){
throw new Error(err)
}
});
});
app.post('Populating Front End with stuff',(req,res)=>{
db.query(`SELECT "userName" FROM snackify WHERE snackphoto IS NOT NULL;
SELECT snackphoto FROM snackify;
SELECT votes FROM snackify WHERE snackphoto IS NOT NULL;
SELECT comments FROM snackify WHERE snackphoto IS NOT NULL;
`,(err,result)=>{
if(err){
throw new Error(err)
}
});
})
// app.post('/submission',(req,res)=> {
// db.query(`UPDATE snackify SET votecount = votecount - 1 WHERE '${req.user.userName}';`, (err, result) => {
// if(err){
// throw new Error(err)
// }
// });
// db.query('UPDATE snackify SET votes = votes + 1 WHERE ;', (err, result) => {
// if(err){
// throw new Error(err)
// }
// });
// });
app.listen(3000, () => {
console.log('listening on port 3000...');
});
})