-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (68 loc) · 2.38 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
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const exp = require("constants");
const methodeOverride = require("method-override");
const ejsMate = require("ejs-mate");
const flash = require('connect-flash');
const session = require("express-session");
const passport = require("passport")
const LocalStrategy = require("passport-local");
const listingRouter = require("./routes/listings.js");
const userRouter = require("./routes/users.js");
const reviewRouter = require("./routes/reviews.js");
const isRedirectUrl = require("./middlewares.js")
const User = require("./models/user.js");
const app = express();
app.set("view engine","ejs");
app.set("views",path.join(__dirname,"views"));
app.use(express.static(path.join(__dirname,"/public")));
app.use(express.urlencoded({extended:true}));
app.use(methodeOverride("_method"));
app.engine("ejs",ejsMate)
PORT =8080;
// Session is set
// app.use(session({
// secret: 'keyboard cat',
// resave: false,
// saveUninitialized: true,
// cookie: { secure: false } // Change this to false if not using HTTPS
// }))
app.use(flash());
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
// use static serialize and deserialize of model for passport session support
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req,res,next)=>{
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
res.locals.currUser = req.user;
next();
})
main().then(()=>{
console.log("Mongo Connection establised");
}).catch((err)=>{
console.log(err);
})
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/shopgrocery');
}
// All Routers site
app.use("/listings",listingRouter)
app.use("/listings/:id/review",reviewRouter);
app.use("/",userRouter);
// Home Route
app.get("/",(req,res)=>{
res.send(`This is the Home Page`);
})
// error Middleware
app.use((err,req,res,next)=>{
let{statusCode = 500,message="Error Not Found"} =err;
res.status(statusCode).render("listings/error.ejs",{message});
})
app.listen(PORT,(req,res)=>{
console.log(`Successfully Started http://localhost:${PORT}/listings`);
})