-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
108 lines (93 loc) · 3.19 KB
/
Copy pathserver.js
File metadata and controls
108 lines (93 loc) · 3.19 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
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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const fetch = require('node-fetch');
const cors = require('cors');
const passport = require('passport');
const db = require('./models');
var Sequelize = require('sequelize'), sequelize = null
const http = require('http')
const ensureAuthenticated = require('./middleware/ensureAuthenticated');
const spotifyStrategy = require('./config/spotifyStrategy');
const heartbeat = require('./router/heartBeat');
const login = require('./auth/spotauth');
const dashBoard = require('./api/dashboard');
const spotifyToken = require('./api/spotifyToken');
const leaderboard = require('./router/leaderboard');
const score = require('./router/score');
const gitHubStrategy = require('./config/gitHubStrategy');
const gitAuth = require('./auth/gitAuth');
// checks if env is Heroku, if so, sets sequelize to utilize the database hosted on heroku
if (process.env.DATABASE_URL) {
// the application is executed on Heroku ... use the postgres database
sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres',
protocol: 'postgres'
})
}
const app = express();
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect();
client.query('SELECT table_schema,table_name FROM information_schema.tables;', (err, res) => {
if (err) throw err;
for (let row of res.rows) {
console.log(JSON.stringify(row));
}
client.end();
});
// `const { Pool } = require('pg');
// const pool = new Pool({
// connectionString: process.env.DATABASE_URL,
// ssl: {
// rejectUnauthorized: false
// }
// }); `
// attach passport to express and sessions
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
session({
secret: 'super secret',
keys: ['key1', 'key2'],
cookie: { maxAge: 60000 },
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(spotifyStrategy);
// passport.use(gitHubStrategy);
app.use('/', express.static(__dirname + '/public'));
app.use('/js', express.static(__dirname + '/js'));
app.use('/css', express.static(__dirname + '/css'));
app.get('/', (req, res) => {
res.json({
is: 'MAIN PAGE',
});
});
heartbeat(app);
login(app, passport);
// gitAuth(app,passport);
dashBoard(app, ensureAuthenticated);
spotifyToken(app, fetch);
leaderboard(app);
score(app);
app.listen(process.env.PORT, () => {
console.log(`The server is running at port ${process.env.PORT}`);
});
// at the bottom of your script, this sets your server to listen for requests, after sequelize has been synced.
// so if you already have your server listening for requests, maybe delete that code. I think, IDK i just copied this
// off of documentation
// db. is assuming you already set sequelize on db
db.sequelize.sync().then(function() {
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
});