-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
100 lines (83 loc) · 2.94 KB
/
server.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
/* eslint-disable global-require */
require('dotenv').config();
const exp = require('express');
const createError = require('http-errors');
const app = exp();
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const session = require('express-session');
// MIDDLEWARE configuration ============================================================
// set up our express application
app.use(morgan('dev')); // Log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({
extended: true,
}));
// parse application/json
app.use(bodyParser.json());
// TEMPLATE configuration ===============================================================
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: 'hbs',
helpers: require('handlebars-helpers')(),
}));
app.set('view engine', 'hbs');
// Static content
app.use(exp.static('./public'));
// Database configuration ===============================================================
const mongoose = require('mongoose');
const dbConfig = require('./src/config/database');
mongoose.connect(dbConfig.uri, {
useNewUrlParser: true,
}); // connect our database
mongoose.set('debug', true);
// configure sessions ==================================================================
app.use(session({
secret: process.env.SECRET,
cookie: { maxAge: 3600000 },
resave: true,
saveUninitialized: true,
}));
// routes ==============================================================================
// set layout variables
app.use((req, res, next) => {
// so we can check if user is logged in
res.locals.user = req.session.user;
next();
});
// routes ==============================================================================
// load our routes and pass to our app
require('./controllers/home')(app);
require('./controllers/users')(app);
require('./controllers/favorites')(app);
require('./controllers/dashboard')(app);
require('./controllers/cart')(app);
// 404 page
app.get('*', (req, res) => {
res.render('error/index');
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler ========================================================================
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => { // DO NOT REMOVE next
// set locals, only providing error in development
console.log('inside error handler!');
console.log(err);
res.locals.message = err.message;
res.locals.error = err;
// render the error page
res.status(err.status || 500);
res.render('error/index');
});
// launch =============================================================================
const { PORT } = process.env;
app.listen(PORT, () => {
console.log(`Keto server listening on ${PORT}`);
});
// We export the app so the tests have access to the server
module.exports = app;