-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
93 lines (85 loc) · 2.36 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
const express = require("express");
let bodyParser = require("body-parser");
let path = require("path");
const app = express();
let mysql = require("mysql");
let passport = require("./server/models/passport");
let authRoutes = require("./routes/authRoutes");
let tripRoutes = require("./routes/tripRoutes");
let expressJWT = require("express-jwt");
let ensureAuthenticated = expressJWT({ secret: "Elirans$uperC0mpl3xKey1337" });
// tell the app to look for static files in these directories
app.use(express.static("./server/static/"));
app.use(express.static("./client/dist/"));
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
// FACEBOOK
// passport initialize
app.use(passport.initialize());
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "test",
database: "routesss"
});
app.use("/auth", authRoutes);
app.use("/trip", tripRoutes);
// for finding
app.post("/getnexthub", function(req, res) {
console.log(req.body);
// res.json(req.body);
if (!req.body.currentHub.hub) {
res.send("no hub sent for search..");
return;
}
let where = { name: req.body.currentHub.hub };
let where2 = "1=1";
if (req.body.type) {
where2 = { type: req.body.type };
}
connection.query(
"SELECT country, locations.id, locations.name,lat," +
"lng,trip_id,trips.name as trip_name, trips.type as trip_type" +
" FROM locations inner join trips on trips.id = locations.trip_id" +
" WHERE trip_id IN ( SELECT trip_id FROM locations WHERE ?) and ?",
[where, where2],
function(err, result) {
if (err) throw err;
console.log(result);
let obj = {};
for (let i = 0; i < result.length; i++) {
if (obj[result[i].trip_id]) {
obj[result[i].trip_id].locations.push(result[i]);
} else {
obj[result[i].trip_id] = {
name: result[i].trip_name,
type: result[i].trip_type,
locations: []
};
obj[result[i].trip_id].locations.push(result[i]);
}
}
let arr = [];
for (let id in obj) {
arr.push(obj[id]);
}
console.log(arr);
res.send(arr);
}
);
});
//Handle browser refresh by redirecting to index html
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "./server/static/index.html"));
});
// connection.end();
// start the server
app.listen(3000, () => {
console.log(
"Server is running on http://localhost:3000 or http://127.0.0.1:3000"
);
});