-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
66 lines (54 loc) · 2.04 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
//Requiring our dependencies
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const cors = require('cors');
const morgan = require('morgan');
const dotenv = require('dotenv');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const connectDb = require('./config/database');
const homeRoutes = require('./routes/home');
const authRoutes = require('./routes/auth');
const postsRoute = require('./routes/posts');
const quotesRoutes = require('./routes/quotes');
const methodOverride = require('method-override')
// initialize express
const app = express();
//Load config setting the .env path to /config/.env
dotenv.config({ path: './config/.env' });
// Load passport config
require('./config/passport')(passport);
// connect to database
connectDb();
//Calling our packages
app.set('view engine', 'ejs'); // sets the view engine to render our ejs
app.use(express.static('public')); // tells express to serve up these 'static' files
app.use(cors()); //allows for cross origin resource sharing(Not used for this project but very important so make it a habit)
app.use(morgan('dev')); //logging middleware. Check console for logs
//body-parsing middleware (required to parse incoming JSON)
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"))
//initialize express sessions - used to persist connection to db
app.use(
session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
// initialize passport sessions - persists auth until user logs out
app.use(passport.initialize());
app.use(passport.session());
// // Routes
app.use('/', homeRoutes);
app.use('/auth', authRoutes);
app.use('/post', postsRoute);
app.use('/quotes', quotesRoutes);
//Initializing our PORT
let PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.... you better go catch it`);
});