-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
105 lines (97 loc) · 3.13 KB
/
Server.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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");
const LogIn = require('./Router/Authentication/LogInRouter');
const PasswordReset = require('./Router/Authentication/PasswordResetRouter');
const Registration = require('./Router/Authentication/RegisterRouter');
const Product = require('./Router/Product/ProductRouter');
const Customer = require('./Router/Customers/CustomerRouter');
const Dealer = require('./Router/Customers/DealerRoute');
const PageLoad = require('./Router/Authentication/LoadingRouter');
const session = require('express-session');
const Passport = require('passport');
const passport = require("passport");
const MongoStore = require('connect-mongo')(session);
const Cors = require('cors');
const { SlowBuffer } = require("buffer");
require('dotenv').config();
const App = express();
const port = process.env.PORT || 5000;
App.use(bodyParser.urlencoded({extended: false}));
App.use(bodyParser.json());
if (process.env.NODE_ENV === "production") {
console.log("Server path", __dirname);
App.use(express.static(path.join(__dirname, "client/build")));
App.enable("trust proxy", 1);
}
else {
App.use(express.static(path.join(__dirname, '/client/public')));
}
App.use(Cors({
origin: true,
credentials: true
}));
App.use(session({
name:"application_session",
resave: false,
saveUninitialized: false,
secret: '12eghdjfksdjf34r32423ekasjdnkasjd',
proxy: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
cookie: {
httpOnly: true,
sameSite: "none",
secure: true,
maxAge: 45000000
}
}));
App.use(passport.initialize());
App.use(passport.session());
App.use('*', function Authentication(req,res,next){
res.header("Access-Control-Expose-Headers", "Is-UserloggedIn");
if(req.isAuthenticated())
{
res.setHeader("Is-UserloggedIn","true");
next();
}
else
{
res.setHeader("Is-UserloggedIn","false");
next();
}
});
App.use('/Authentication/User', LogIn);
App.use('/Authentication/PasswordReset', PasswordReset);
App.use('/Authentication/RegisterUser', Registration);
App.use('/Product' , Product);
App.use('/Customer' , Customer);
App.use('/Dealer' , Dealer);
App.use('/', (req, res) => {
let option = {
headers:
{
"x-timestamp": Date.now(),
"Is-UserloggedIn": false
}
}
res.header("Access-Control-Expose-Headers", "Is-UserloggedIn");
if (process.env.NODE_ENV === "production" && req.isAuthenticated())
{
option.headers["Is-UserloggedIn"] = true;
return res.status(200).sendFile(path.join(__dirname , './client/build/index.html'),option)
}
else
{
return res.status(200).sendFile(path.join(__dirname , './client/public/index.html'), option)
}
});
App.listen(port, async function ConnectDB(){
return await mongoose.connect(process.env.MONGO_URI)
.then()
.catch( error => {
console.log("error", error);
})
});