-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (30 loc) · 1.03 KB
/
Copy pathserver.js
File metadata and controls
38 lines (30 loc) · 1.03 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
import path from "path";
import express from "express";
import { fileURLToPath } from "url";
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Backend API routes
app.get("/api/hello", (req, res) => {
console.log(req.method, req.url);
res.json({ message: "Hello from Node.js backend!" });
});
app.get("/api/hello2", (req, res) => {
console.log(req.method, req.url);
res.json({ message: "Hello2 from Node.js backend!" });
});
app.get("/api/hello3", (req, res) => {
console.log(req.method, req.url);
res.json({ message: "Hello3 from Node.js backend!" });
});
// Frontend static files
app.use(express.static(path.join(__dirname, "dist")));
// Serve the frontend with index.html for all other routes
// this helps with client-side routing
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`API server running on http://localhost:${PORT}`);
});