-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (45 loc) · 1.43 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
const express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
userRoutes = require('./routes/users'),
methodOverride = require('method-override'),
postRoutes = require('./routes/posts'),
commentRoutes = require('./routes/comments'),
path = require('path'),
session = require('express-session'),
app = express(),
port = process.env.PORT || 3000,
url = process.env.DATABASEURL || 'mongodb://localhost:27017/learn_forever';
// CONFIG SESSION
app.use(session({
secret: 'Shauritanga loves you all',
resave: false,
saveUninitialized: false
}));
// make user available to all template
app.use((req, res, next) => {
res.locals.currentUser = req.session.userId;
next();
})
//CONFIG APP
app.use(methodOverride('_method'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
// CONNCTING TO DB
mongoose.connect(url, {useNewUrlParser: true, useCreateIndex: true});
//landing page
app.get('/', function(req, res) {
res.render('index', {
title: 'Home',
path: '/'
})
})
//CONFIG ROUES
app.use('/users', userRoutes);
app.use(commentRoutes);
app.use('/posts', postRoutes);
//LISTENING ON PORT
app.listen(port, function() {
console.log(`Server is running on port ${port}`)
});