-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (85 loc) · 2.43 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
let express = require("express");
let partials = require("express-partials");
let app = express();
var session = require("express-session");
const mysql = require("mysql");
const cors = require("cors")({ origin: true });
const bodyParser = require("body-parser");
const path = require("path");
var Cookies = require("cookies");
const auth = require("./helper/auth_helper");
let apiRoutes = require("./routes/api-routes");
let appRoutes = require("./routes/app-routes");
var port = process.env.PORT || 5000;
app.set("view engine", "ejs");
app.use(
session({
secret: "app",
cookie: { maxAge: 6000 },
resave: true,
saveUninitialized: true
})
);
app.use(partials());
app.use(express.static(path.join(__dirname, "public")));
app.use(cors);
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
// app.use(express.urlencoded({ extended: true }));
// app.use(express.json());
const connectToDB = (req, res, next) => {
const db = mysql.createConnection({
host: "remotemysql.com",
user: "a2AVResZMP",
password: "pOXXNddV6L",
database: "a2AVResZMP",
multipleStatements: true
});
// const db = mysql.createConnection({
// host: "localhost",
// user: "root",
// password: "",
// database: "lawyer"
// });
db.connect(err => {
if (err) {
throw err;
res.json({ status: 0, message: "DB connection error" });
}
});
console.log("Connected to database");
global.db = db;
next();
};
var checkUser = (req, res, next) => {
if (req.url.includes("/account")) {
console.log("In account url");
var keys = ["lawyer_auth"];
var cookies = new Cookies(req, res, { keys: keys });
var clientLoggedIn = cookies.get("clientLoggedIn", { signed: true });
var clientId = cookies.get("clientId", { signed: true });
console.log("Clientlogged in " + clientLoggedIn);
console.log("Clientid " + clientId);
}
console.log("In check user");
next();
};
const closeConnection = (req, res, next) => {
db.end(function(err) {
console.log("connection is close now");
});
};
app.use(connectToDB);
// app.use(checkUser);
app.use(auth.clientAuth);
app.use("/", appRoutes);
app.use("/api", apiRoutes);
app.use("/account", appRoutes);
app.use(closeConnection);
app.listen(port, function() {
console.log("app running on port " + port);
});