-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
126 lines (110 loc) · 4.23 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// CALLING ENVIRONMENTAL VARIABLE MODULE
require("dotenv").config();
// CALLING DEPENDECNIES IN PACKAGE.JSON
const express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/user"),
MongoClient = require("mongodb"),
morgan = require("morgan"),
cors = require("cors"),
winston = require("winston"),
helmet = require("helmet"),
compression = require("compression");
Sentry = require("@sentry/node");
Tracing = require("@sentry/tracing");
// CALLING ROUTES
const commentRoutes = require("./routes/comments"),
reviewRoutes = require("./routes/reviews"),
campgroundRoutes = require("./routes/campgrounds"),
indexRoutes = require("./routes/index");
mongoose.set("useNewUrlParser", true);
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
mongoose.set("useUnifiedTopology", true);
// linking the database here with the one on Heroku
var url = process.env.DATABASEURL;
// Use the database below when in production
// "mongodb://localhost/temporary_database"
// checking DATABASEURL value
//console.log(process.env.DATABASEURL);
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true
}). then (() => {
console.log("Conencted to DB");
}). catch((err) => {
console.log("ERROR:", err.message);
});
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public")); // Using __dirname + is a better way to navigate to the file
app.set("view engine", "ejs");
app.use(methodOverride("_method"));
app.use(flash());
// Uncomment the code below for additional logging during development
//app.use(cors()); // CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options
//app.use(morgan("combined")); // HTTP request logger middleware for node.js. Use during development
// Helmet helps you secure your Express apps by setting various HTTP headers
app.use(
helmet({
contentSecurityPolicy: false,
})
);
// CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
// const whitelist = ["https://goorm-ide-test-ngvdz.run-us-west2.goorm.io/", "https://aqueous-reaches-28926.herokuapp.com/", "http://goorm-ide-test-ngvdz.run-us-west2.goorm.io/"];
// const corsOptions = {
// origin: function (origin, callback) {
// if (whitelist.indexOf(origin) !== -1) {
// callback(null, true);
// } else {
// callback(new Error("Not allowed by CORS"));
// }
// }
// }
// app.use(cors(corsOptions));
// compress all responses
app.use(compression());
// Moment JS for timestamps
app.locals.moment = require("moment");
// Passport Configuration
app.use(require("express-session")({
secret: process.env.PASSPORT_SECRET,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Middleware used below
app.use(function(req, res, next){
res.locals.currentUser = req.user; // passing user into every single template
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use(indexRoutes);
app.use("/campgrounds", campgroundRoutes);
app.use("/campgrounds/:id/comments", commentRoutes);
app.use("/campgrounds/:id/reviews", reviewRoutes);
// Sentry Error Logging
Sentry.init({
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
const port = process.env.PORT || 3000;
app.listen(port, process.env.IP, function(){
console.log("Server is on");
console.log("Application is live at http://localhost:3000");
});