-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (106 loc) · 3.47 KB
/
app.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
const mongoose = require('mongoose').default;
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const hbs= require('hbs');
const app = express();
const session = require('express-session');
const crypto = require('crypto');
const { comparePasswords } = require("./hashing");
const User = require('./models/UsersDB');
const userRoutes = require('./routes/users');
const ideasRoutes = require('./routes/ideas');
const loginRoutes = require('./routes/login');
const registerRoutes = require('./routes/register');
const profileRoutes = require('./routes/profile');
const ideasAPI = require('./routes/api/ideasAPI');
const teamsAPI = require('./routes/api/teamsAPI');
const profileAPI = require('./routes/api/profileAPI');
const skillsAPI = require('./routes/api/skillsAPI');
const path = require("path");
const teamsRoutes = require('./routes/teams')
const usernameAPI = require('./routes/api/usernameAPI');
const myTeamsRoutes = require('./routes/myTeams');
const logoutRoutes = require('./routes/logout');
hbs.registerHelper("repeat", function (times, options) {
let result = '';
for (let i = 0; i < times; i++) {
result += options.fn(this);
}
return result;
})
hbs.registerHelper('isLessThan', function (value, compareValue, options) {
if (value < compareValue) {
return options.fn(this);
}
return options.inverse(this);
});
const secret = crypto.randomBytes(64).toString('hex');
app.use(session({
secret: secret,
resave: false,
saveUninitialized: true
}));
app.set('view engine', 'hbs');
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(
async function(username, password, done) {
try {
const user = await User.findOne({ username: username });
if (user && await comparePasswords(password, user.password)) {
return done(null, user);
} else {
return done(null, false);
}
} catch (err) {
return done(err);
}
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
const PORT = process.env.PORT || 3000;
const DB_URI = process.env.DB_URI || 'mongodb://127.0.0.1:27017/gipdb';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
mongoose.connect(DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('Database connected!'))
.catch((err) => console.log(err));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/users', userRoutes);
app.use('/login', loginRoutes);
app.use('/ideas', ideasRoutes);
app.use('/teams', teamsRoutes)
app.use('/register', registerRoutes);
app.use('/profile', profileRoutes);
app.use('/myteams', myTeamsRoutes);
app.use('/logout', logoutRoutes);
app.use('/api/ideas', ideasAPI);
app.use('/api/teams', teamsAPI);
app.use('/api/profile', profileAPI);
app.use('/api/skills', skillsAPI);
app.use('/api/username', usernameAPI);
app.set('views', './views')
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
app.get('/', (req, res) => {
res.redirect('/ideas');
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}...`);
});