-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.48 KB
/
index.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
require('dotenv').config();
const express = require('express');
const connectToMongo = require('./config/db');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const userRoutes = require('./routes/userRoutes');
const githubRoutes = require('./routes/githubRoutes');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
connectToMongo();
app.use(session({
secret: process.env.SESSION_SECRET || 'mysecretkey',
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI || 'mongodb://localhost:27017/stars' }),
cookie: { secure: false }
}));
app.use(function(req, res, next) {
res.locals.session = req.session;
next();
});
app.use(function(req, res, next) {
const publicPaths = ['/user/login', '/user/register'];
if (req.session.userId && publicPaths.includes(req.path)) {
return res.redirect('/user/homepage');
}
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/user', userRoutes);
app.use('/github', githubRoutes);
app.get('/', (req, res) => {
res.redirect('/user/login');
});
app.listen(port, () => {
console.log('Server running on port ' + port);
});