forked from webdevpt-0921/course-project-module-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (53 loc) · 1.55 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
const express = require('express');
const handlebars = require('hbs');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const cookieParser = require('cookie-parser');
const { MONGO_URI } = require('./db/index');
const baseRoutes = require('./routes/base');
const authRoutes = require('./routes/auth');
const courseRoutes = require('./routes/course');
const { isLoggedIn } = require('./middlewares');
handlebars.registerPartials(`${__dirname}/views/partials`);
function setupApp() {
const app = express();
app.set('view engine', 'hbs');
// app.set("views", __dirname + "/pages");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(cookieParser());
app.use(
session({
store: MongoStore.create({
mongoUrl: MONGO_URI,
ttl: 24 * 60 * 60,
}),
secret: process.env.SECRET,
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000,
},
}),
);
app.use('/', baseRoutes());
app.use('/auth', authRoutes());
app.use('/course', isLoggedIn, courseRoutes());
app.use((req, res) => {
console.log('404');
res.render('404.hbs');
});
app.use((error, req, res, next) => {
let err;
if (req.app.get('env') === 'development') {
err = error;
} else {
err = {};
}
// req.app.get('env') === 'development' ? err : {};
res.render('error', { message: error.message, error: err });
});
return app;
}
module.exports = setupApp;