-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (65 loc) · 2.82 KB
/
server.js
File metadata and controls
77 lines (65 loc) · 2.82 KB
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
// server.js
require('dotenv').config(); // load .env first
// set up ======================================================================
// get all the tools we need
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const morgan = require('morgan');
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const path = require('path');
const expressLayouts = require('express-ejs-layouts');
// passport config ======================================================================
require('./config/passport')(passport);
// database connection ======================================================================
const dbURL = process.env.MONGO_URI || 'mongodb://localhost:27017/communityTradeDB';
mongoose.connect(dbURL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Great! MongoDB connected'))
.catch(err => console.log('Oh no! MongoDB connection error:', err));
// middleware ======================================================================
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(
{ extended: true }
));
// sessions ======================================================================
app.use(session({
secret: process.env.SESSION_SECRET || 'swap-secret',
resave: false,
saveUninitialized: false,
cookie: {
secure: false
} // set to true if using HTTPS
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// static files & EJS ======================================================================
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.set('view engine', 'ejs');
app.use(expressLayouts);
app.set('layout', 'layout');
// user available in all templates ======================================================================
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
// routes ======================================================================
require('./app/routes')(app, passport);
// start server ======================================================================
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(` Server running on port ${PORT}`));
//Citations:
//Modified code from youtube tutorial: https://www.youtube.com/watch?v=z5UgtXOxEEk
//Reference code from https://www.mongodb.com/resources/languages/express-mongodb-rest-api-tutorial#setting-up-the-project
//Use of dotenv package to hide sensitive info: https://www.npmjs.com/package/dotenv
//Use of Learning Mode on AI tools to help with code structure,syntax and debugging