-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·87 lines (64 loc) · 1.88 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
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
require('./models/User');
require('./models/Article');
require('./config/passport')(passport);
const auth = require('./routes/auth');
const index = require('./routes/index');
const articles = require('./routes/articles');
const keys = require('./config/keys');
const {
truncate,
stripTags,
formatDate,
editIcon,
} = require('./helpers/hbs');
mongoose.Promise = Promise;
const env = process.env.NODE_ENV;
mongoose.connect(keys[env].mongoURI, {
useMongoClient:true
})
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.engine('handlebars', exphbs({
helpers: {
truncate: truncate,
stripTags: stripTags,
formatDate: formatDate,
editIcon: editIcon
},
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
app.use(cookieParser());
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/auth', auth);
app.use('/articles', articles);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server started on port ${port}`)
});
module.exports = app;