WIP backend improvements

This commit is contained in:
Alan O'Cull 2023-12-05 16:48:02 -05:00
parent 193f495e00
commit 5646c84a89
3 changed files with 40 additions and 19 deletions

View File

@ -3,7 +3,7 @@
<head> <head>
<title>Pattern Packer</title> <title>Pattern Packer</title>
<link rel="stylesheet" href="../css/puzzle.css"> <link rel="stylesheet" href="css/puzzle.css">
<script src="client.js"></script> <script src="client.js"></script>
</head> </head>
<body> <body>

View File

@ -1,13 +1,36 @@
import { Mesh, MeshImport } from "./mesh"; import { Mesh, MeshImport } from "./mesh";
const request_mesh_handler = (e: ProgressEvent<XMLHttpRequestEventTarget>) => {
console.log(`XML REQUEST ${e.type}: ${e.loaded} bytes transferred\n`);
}
const request_mesh_listeners = (xhr: XMLHttpRequest) => {
xhr.addEventListener("loadstart", request_mesh_handler);
xhr.addEventListener("load", request_mesh_handler);
xhr.addEventListener("loadend", request_mesh_handler);
xhr.addEventListener("progress", request_mesh_handler);
xhr.addEventListener("error", request_mesh_handler);
xhr.addEventListener("abort", request_mesh_handler);
}
const request_mesh = (filename: string) => { const request_mesh = (filename: string) => {
let request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open('GET', `http://localho.st:${3000}/mesh/${filename}`, false); request_mesh_listeners(request);
request.onreadystatechange = function() {
if (request.readyState == XMLHttpRequest.DONE) {
alert(request.responseText);
const mesh = MeshImport.parse(request.responseText);
console.log(`Got mesh array at ${mesh.name}`);
console.log(mesh.shells);
}
}
request.open('GET', `http://localhost:${3000}/mesh/${filename}`, true);
request.send(null); request.send(null);
return request.responseText; return request;
} }
const buff = request_mesh('clover.obj'); document.onload = function() {
const mesh = MeshImport.parse(buff); console.log('PAGE LOADED');
console.log(`Got mesh array at ${mesh.name}`); const req = request_mesh('clover.obj');
console.log(mesh.shells); }

View File

@ -8,19 +8,17 @@ dotenv.config();
const app: Express = express(); const app: Express = express();
const port = process.env.PORT; const port = process.env.PORT;
app.get('/', (req: Request, res: Response) => { app.get('/mesh/*', (req: Request, res: Response) => {
console.log(req.url); console.log('Got MESH request ', req.url);
console.log(req.body); res.header("Access-Control-Allow-Origin", "*");
console.log(req.query); res.header("Access-Control-Allow-Headers", "X-Requested-With");
const last = req.url.lastIndexOf('/'); app.use(express.static('dist/public/objects/hard'));
const mesh_name = req.url.substring(last + 1); });
const filepath = `assets/pages/${mesh_name}`;
if (fs.existsSync(filepath)) { app.get('/', (req: Request, res: Response) => {
fs.readFileSync(`assets/pages/${mesh_name}`).toString('utf8'); console.log('Got MAIN request ', req.url);
} else {
res.send('o hello world'); app.use(express.static('dist' + '/public'));
}
}); });
app.listen(port, () => { app.listen(port, () => {