-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (85 loc) · 2.47 KB
/
index.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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const port = process.env.PORT || 3000;
const ejsmate = require("ejs-mate");
const path = require("path");
const session = require("express-session");
const methodOverride = require("method-override");
const passport = require("passport");
const LocalStrategy = require("passport-local");
const passportLocalMongoose = require("passport-local-mongoose");
const User = require("./models/user");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const flash = require("connect-flash");
const cookieParser = require('cookie-parser')
// const cors = require("cors")
// app.use(
// cors({
// origin: "http://localhost:3000/",
// })
// )
// Passport Config
require('./config/passport')(passport);
//Setting Up mongoose
//mongodb://127.0.0.1/keyhole -- localhost:27010
mongoose
.connect("mongodb+srv://uraj212001:[email protected]/users_keyhole?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log(`connection successful`);
})
.catch((error) => {
console.log(error);
console.log(`no connection`);
});
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: true }));
// Express session
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Connect flash
app.use(flash());
// Global variables
app.use(function(req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
});
app.listen(port, () => {
console.log("listning!" + port);
});
app.use(session({ cookie: { maxAge: 60000 },
secret: 'woot',
resave: true,
saveUninitialized: true}));
app.use(flash());
//Routes
app.use('/', require('./routes/user.js'));
app.engine("ejs", ejsmate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "/public")));
app.use(methodOverride("_method"));
app.use((err, req, res, next) => {
let { status = 500, message = "Error Occurred!" } = err;
console.log(err);
res.status(status).send(message);
});
app.get("*", (req, res) => {
res.status(404).send("404 Not Found!");
});