-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (81 loc) · 2.25 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
import 'dotenv/config';
import express from 'express';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import compression from 'compression';
import helmet from 'helmet';
import cors from 'cors';
import createErrors from 'http-errors';
import session from 'express-session';
import MongoStore from 'connect-mongodb-session';
import passport from 'passport';
import database from './utils/database';
import rateLimit from './utils/rateLimit';
// import './utils/passport';
import indexRouter from './routes/index';
import usersRouter from './routes/users';
import postsRouter from './routes/frontend';
import authorRouter from './routes/author';
const app = express();
app.use(
cors({
origin: 'http://localhost:5173',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true,
})
); //Enable CORS
app.use(rateLimit); //Rate Limiting
app.use(database); //Database Connection
app.use(
helmet.contentSecurityPolicy({
directives: {
'script-src': 'self',
},
})
);
const MongoStoreSession = MongoStore(session);
const store = new MongoStoreSession({
uri: `mongodb+srv://${process.env.MONGODB_USER}:${process.env.MONGODB_PASS}@cluster0.dmc0his.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`,
collection: 'sessions',
});
store.on('error', function (error) {
console.log(error);
});
// Passport
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: store,
cookie: {
maxAge: 1000 * 60 * 60 * 24, // 1 day
},
})
);
app.use(passport.initialize());
app.use((req, res, next) => {
console.log(
req.session.viewCount ? req.session.viewCount : 'viewcount not found',
' ---- ',
req.sessionID,
req.session
);
next();
});
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/posts', postsRouter);
app.use('/author', authorRouter);
app.use(compression()); //Compress all routes
app.use(function (req, res, next) {
next(createErrors(404));
});
export default app;