-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
126 lines (100 loc) · 2.99 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require('dotenv').config();
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const path = require("path");
const ejsMate = require("ejs-mate");
const bodyParser = require("body-parser");
const ExpressError = require("./utils/ExpressError.js");
const passport = require("passport");
const localStartegy = require("passport-local");
const flash = require("connect-flash");
const session = require("express-session");
const user = require("./dbmodels/user.js");
const searchRouter = require("./routes/search.js");
const aiRouter = require('./routes/ai.js');
const userRouter = require('./routes/user.js');
app.use(express.json());
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(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.engine('ejs', ejsMate);
const sessionOption = {
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
cookie: {
expiers: Date.now() + 7 * 24 * 60 * 60 * 1000,
maxAge: 7 * 24 * 60 * 60 * 1000,
httpOnly: true,
},
};
app.listen(3000, () => {
console.log("Server is working");
});
app.use(session(sessionOption));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStartegy(user.authenticate()));
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();
});
const url = process.env.URL_DB;
async function main() {
await mongoose.connect(url);
}
main().then(() => {
console.log("Connected to DB");
}).catch((err) => {
console.log(err);
});
app.use("/home", searchRouter);
app.use("/ai", aiRouter);
app.use('/user', userRouter);
app.get('/', (req,res)=>{
res.redirect('/home');
});
app.get("/chart", (req, res) => {
res.render('chart.ejs');
})
//BASIC ROUTES
app.get('/test', (req, res) => {
res.render('main/skynetAI.ejs');
})
app.get("/about", (req, res) => {
res.render("Basic/journey.ejs");
});
// app.get('/image', async (req, res) => {
// const images = await getreq();
// console.log(images);
// res.render('main/imagesearch.ejs', { images });
// });
app.get("/contact", (req, res) => {
res.render("Basic/contact.ejs");
});
app.get("/company", (req, res) => {
res.render("Basic/companypage.ejs");
});
app.post("/feedback", async (req, res) => {
//To Be Written
});
app.get("/comingsoon", (req, res) => {
res.render("main/coming.ejs");
});
//ERROR Routes
app.all("*", (req, res, next) => {
next(new ExpressError(404, "Page Not Found!!"));
});
app.use((err, req, res, next) => {
let { statusCode = 500, message = "Something Went Wrong!!" } = err;
res.status(statusCode).render("error.ejs", { err });
});