-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (37 loc) · 1021 Bytes
/
server.js
File metadata and controls
47 lines (37 loc) · 1021 Bytes
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
const express = require("express");
const cors = require("cors");
const path = require("path");
const app = express();
const PORT = 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(__dirname)); // serve driver.html & user.html
// store driver location
let driverLocation = {};
// serve driver page
app.get("/driver", (req, res) => {
res.sendFile(path.join(__dirname, "driver.html"));
});
// serve user page
app.get("/user", (req, res) => {
res.sendFile(path.join(__dirname, "user.html"));
});
// driver updates location
app.post("/location", (req, res) => {
const { lat, lng, accuracy } = req.body;
driverLocation = {
lat,
lng,
accuracy: accuracy || null,
timestamp: Date.now()
};
console.log("Updated location:", driverLocation);
res.json({ status: "ok" });
});
// user requests driver location
app.get("/location", (req, res) => {
res.json(driverLocation);
});
app.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});