-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·89 lines (67 loc) · 2.49 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
//initiate express
var express = require("express"),
app = express(),
//initiate body-parser
bodyParser = require("body-parser"),
//initiate method-override
methodOverride = require("method-override"),
//initiate express-sanitizer to remove scripts
expressSanitizer = require("express-sanitizer"),
//initiate passport
passport = require("passport"),
localStrategy = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
//initiate flash
flash = require("connect-flash"),
//initiate schemas
Order = require("./models/order"),
Price = require("./models/price"),
User = require("./models/user");
//initiate route files
var orderRoutes = require("./routes/orders"),
authRoutes = require("./routes/auth"),
priceRoutes = require("./routes/prices");
//initiate mongoose, replace username and password with your own
var mongoose = require("mongoose");
mongoose.connect("mongodb+srv://username:[email protected]/order_app?retryWrites=true", {useNewUrlParser: true});
// mongoose.connect("mongodb://localhost/order_app", {useNewUrlParser: true});
//app config
//tell express to use body-parser
app.use(bodyParser.urlencoded({extended: true}));
//set view engine to ejs so ejs files can be referred to without typing their file type
app.set("view engine", "ejs");
//for css
app.use(express.static(__dirname + "/public"));
//tell express to use method-override
app.use(methodOverride("_method"));
//tell app to use express-sanitizer
app.use(expressSanitizer());
//tell app to use flash
app.use(flash());
//initiate and tell app to use express-session (passport configuration)
app.use(require("express-session")({
secret: "This can be anything at all",
resave: false,
saveUninitialized: false
}));
//tell app to use passport
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//passes user information to every route
app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
//the following routes follow RESTful conventions
app.use(authRoutes);
app.use(priceRoutes);
app.use(orderRoutes);
//start server
app.listen(process.env.PORT, process.env.IP, function(){
console.log("The WhiteV Order server has started.");
});