-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (125 loc) · 3.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var logger = require("morgan");
var pug = require("pug");
const mongoose = require("mongoose");
var dbBench = require("./user_model.js");
const axios = require("axios");
require("dotenv").config();
const uri = process.env.MONGOSTRING;
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("MongoDB Connected");
})
.catch((err) => console.log(err));
const port = 5000;
var app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use("/public", express.static(path.join(__dirname, "public")));
app.use(logger("dev"));
app.use(express.json());
app.use(
express.urlencoded({
extended: false,
})
);
app.use(express.static(path.join(__dirname, "public")));
app.get("/", async function (req, res, next) {
var dataa = await dbBench.find({}).exec(); //find all entrys
res.render("index", {
data: JSON.stringify(dataa),
});
});
app.get("/add", async function (req, res, next) {
var dataa = await dbBench.find({}).exec(); //find all entrys
res.render("add", {
data: JSON.stringify(dataa),
});
});
app.post("/add", async function (req, res, next) {
console.log(req.body.lat);
console.log(req.body.long);
console.log(req.body.beschreibung);
console.log(req.body.nickname);
if (req.body.nickname == "") {
req.body.nickname = "unknown";
}
dbBench.create({
lat: req.body.lat,
long: req.body.long,
beschreibung: req.body.beschreibung,
nickname: req.body.nickname,
});
console.log("Successful creation of new entry");
res.render("thanks");
});
app.get("/leaderboard", async function (req, res, next) {
var all = await dbBench.aggregate([
{
$group: {
_id: "$nickname",
count: {
$sum: 1,
},
},
},
{
$sort: {
count: -1,
},
},
]);
console.log(all);
res.render("leaderboard", {
tabledata: all,
});
});
app.get("/fullscreenmap", async function (req, res, next) {
var dataa = await dbBench.find({}).exec(); //find all entrys
res.render("fullscreenmap", {
data: JSON.stringify(dataa),
});
});
app.get("/:id", function (req, res, next) {
console.log(req.params.id);
res.render("singlebench", {
id: req.params.id,
});
});
app.get("/profile/:id", async function (req, res, next) {
var all = await dbBench.find({
nickname: req.params.id,
});
console.log(all);
res.render("profile", {
tabledata: all,
nickname: req.params.id,
datajson: JSON.stringify(all),
});
});
app.get("/singleinfo/:id", async function (req, res, next) {
let endpoint =
"https://overpass.openstreetmap.fr/api/interpreter?data=[out:json]; (node(" +
req.params.id +
");); (._;>;); out;";
axios
.get(endpoint)
.then((response) => {
console.log(response.data);
res.send(response.data);
//console.log(response.data.explanation);
})
.catch((error) => {
console.log(error);
});
});
app.listen(process.env.PORT || 5000, () => {
console.log(`App listening at http://localhost:${port}`);
});