-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (73 loc) · 2.07 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
//for env
require("dotenv").config();
//node modules
const express=require("express");
const path=require("path");
const session = require('express-session');
const bodyParser=require("body-parser");
const MongoStore =require('connect-mongo');
const mongoSanitize = require('express-mongo-sanitize');
var xss = require('xss-clean')
const passport = require("./passport/setup");
//utils
const {AppError}=require("./util/util");
//routers
const signinrouter=require("./routes/signin");
const signuprouter=require("./routes/signup");
const userrouter=require("./routes/user");
const apirouter=require("./api/api");
const tokenrouter=require("./routes/token");
const adminrouter=require("./routes/admin");
//db
const dbConnect=require("./dbconnect");
//middleware
const errorHandler=require("./middleware/errorHandler");
//app
const app=new express()
//middleware's
app.use(xss())
app.use(mongoSanitize());
app.use(express.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(session({
secret: 'anbbduwwebsdnnasjsboolovesssbxzsjsobdkdsdn',
resave: false,
saveUninitialized: false,
cookie: { secure: false },
store: MongoStore.create( {mongoUrl:process.env.DBURL})
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// setting value
app.set("view engine" ,"ejs");
app.set("views","./views");
let port=process.env.PORT || 3000;
//connecting to db
async function connect() {
await dbConnect();
console.log("Db sucessfully connected");
}
connect();
//routing
app.use(express.static(path.join(__dirname+"/views")))
app.use("/api",apirouter);
app.use("/signin",signinrouter);
app.use("/signup",signuprouter);
app.use("/user",userrouter);
app.use("/token",tokenrouter);
app.use("/admin",adminrouter);
//error handler
app.use(errorHandler);
app.get("*",(req,res)=>{
res.sendFile(path.join(__dirname+"/views/index.html"));
})
//404 page
app.get("/*",(req,res)=>{
res.render("error");
})
process.on("uncaughtException",(e)=>{
console.log("uncaughtException",e)
});
app.listen(port,()=>{console.log("Server started")});