-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (59 loc) · 1.67 KB
/
app.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
const express = require("express");
const markers = require("./routes/marker");
const authRoutes = require("./routes/loginRoute");
const admin = require("./routes/adminRoutes");
const authorization = require("./middleware/auth");
const mysql = require("mysql");
const cookieParser = require("cookie-parser");
const mysqlConnection = require("./db/connect");
require("dotenv").config();
// Instantiating the server
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const port = process.env.PORT || 8080;
const host = '0.0.0.0';
// Setting middlewares
app.use(express.static("./public"));
app.use(cookieParser("MySecret"));
// Routes
app.use("/api/v1/markers", markers);
app.use("/", authRoutes);
app.use("/admin", authorization, admin);
// listening to port and connecting database
const start = async () => {
try {
mysqlConnection.getConnection((err, connection) => {
if (connection) {
console.log(`Database connected`);
} else {
console.log(err);
}
});
app.listen(port, () => {
console.log(`Server is listening to port: ${port}`);
});
} catch (error) {
console.log(error);
}
};
function handleDisconnect(connection) {
connection.on("error", function (err) {
if (!err.fatal) {
return;
}
if (err.code !== "PROTOCOL_CONNECTION_LOST") {
throw err;
}
console.log("Re-connecting lost connection: " + err.stack);
connection = mysql.createConnection(connection.config);
handleDisconnect(connection);
connection.connect((err) => {
if (!err) {
console.log(`Database reconnected`);
}
});
});
}
// starting the server
start();