-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (85 loc) · 1.93 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
const express = require('express'),
dotenv = require('dotenv'),
morgan = require('morgan'),
connect = require('./config/db'),
passport = require('passport'),
session = require('express-session'),
methodOverride = require('method-override'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo')(session),
hbs = require('express-handlebars');
// Environment
dotenv.config({ path: './config/config.env' });
connect();
const app = express();
const PORT = process.env.PORT || 3000;
// Parser and Method Override
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method;
}
}),
);
// Session
app.use(
session({
secret: 'keyboards',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
}),
);
// Passport
require('./config/passport')(passport);
// Logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
res.locals.user = req.user || null;
next();
});
// Helpers
const {
formatDate,
stripTags,
truncate,
editIcon,
select,
} = require('./helpers/hbs');
// Route
app.use('/', require('./route/index'));
app.use('/auth', require('./route/auth'));
app.use('/stories', require('./route/stories'));
// Handlebars
app.engine(
'.hbs',
hbs({
defaultLayout: 'main',
extname: '.hbs',
helpers: {
formatDate,
stripTags,
truncate,
editIcon,
select,
},
}),
).set('view engine', '.hbs');
app.listen(PORT, (err) => {
if (err) {
throw new Error(err);
}
console.log(
`Server is in ${process.env.NODE_ENV} mode, and running on ${PORT}`,
);
});