forked from hackclub/flavorMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (30 loc) · 1.2 KB
/
server.js
File metadata and controls
37 lines (30 loc) · 1.2 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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 4000;
// Enable CORS for all routes
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// serve statics first
app.use(express.static(__dirname));
// Dedicated route for courtyard map
app.get(['/courtyard', '/courtyard.tmj'], (req, res) => {
res.sendFile(path.join(__dirname, 'courtyard.tmj'), {
headers: { 'Content-Type': 'application/json' }
});
});
// Wildcard route to serve house.tmj after statics
app.get('/*', (req, res) => {
const title = req.params[0]; // Get the title from the URL
res.sendFile(path.join(__dirname, 'house.tmj'), { headers: { 'Content-Type': 'application/json' } });
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});