forked from 2k33cse992574/location-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (40 loc) · 1.3 KB
/
server.js
File metadata and controls
51 lines (40 loc) · 1.3 KB
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
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const path = require("path");
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
// ✅ Middleware
app.use(cors());
app.use(express.json());
// ✅ MongoDB Connection
mongoose.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
})
.then(() => console.log("✅ MongoDB connected"))
.catch((err) => console.error("❌ MongoDB connection error:", err));
// ✅ API Routes
const locationRoutes = require("./routes/location");
app.use("/api/location", locationRoutes);
// ✅ Serve Static Frontend Files
app.use(express.static(path.join(__dirname, "public")));
// ✅ Handle frontend routes (index.html, accept.html, track.html)
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/accept.html", (req, res) => {
res.sendFile(path.join(__dirname, "public", "accept.html"));
});
app.get("/track.html", (req, res) => {
res.sendFile(path.join(__dirname, "public", "track.html"));
});
// ✅ 404 fallback for other requests
app.use((req, res) => {
res.status(404).send("❌ Page not found");
});
// ✅ Start Server
app.listen(PORT, () => {
console.log(`🚀 Server is running on port ${PORT}`);
});