From 5646c84a896086833685ac2e67d823ca91f8ea68 Mon Sep 17 00:00:00 2001 From: Alan O'Cull Date: Tue, 5 Dec 2023 16:48:02 -0500 Subject: [PATCH] WIP backend improvements --- assets/{puzzle.html => index.html} | 2 +- src/client/client.ts | 35 +++++++++++++++++++++++++----- src/server/server.ts | 22 +++++++++---------- 3 files changed, 40 insertions(+), 19 deletions(-) rename assets/{puzzle.html => index.html} (85%) diff --git a/assets/puzzle.html b/assets/index.html similarity index 85% rename from assets/puzzle.html rename to assets/index.html index e4409f7..1fb82d1 100644 --- a/assets/puzzle.html +++ b/assets/index.html @@ -3,7 +3,7 @@ Pattern Packer - + diff --git a/src/client/client.ts b/src/client/client.ts index 40fe368..51660fe 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,13 +1,36 @@ import { Mesh, MeshImport } from "./mesh"; + +const request_mesh_handler = (e: ProgressEvent) => { + 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) => { 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); - return request.responseText; + return request; } -const buff = request_mesh('clover.obj'); -const mesh = MeshImport.parse(buff); -console.log(`Got mesh array at ${mesh.name}`); -console.log(mesh.shells); +document.onload = function() { + console.log('PAGE LOADED'); + const req = request_mesh('clover.obj'); +} diff --git a/src/server/server.ts b/src/server/server.ts index abb43d3..44d76e4 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -8,19 +8,17 @@ dotenv.config(); const app: Express = express(); const port = process.env.PORT; -app.get('/', (req: Request, res: Response) => { - console.log(req.url); - console.log(req.body); - console.log(req.query); - const last = req.url.lastIndexOf('/'); - const mesh_name = req.url.substring(last + 1); - const filepath = `assets/pages/${mesh_name}`; +app.get('/mesh/*', (req: Request, res: Response) => { + console.log('Got MESH request ', req.url); + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + app.use(express.static('dist/public/objects/hard')); +}); - if (fs.existsSync(filepath)) { - fs.readFileSync(`assets/pages/${mesh_name}`).toString('utf8'); - } else { - res.send('o hello world'); - } +app.get('/', (req: Request, res: Response) => { + console.log('Got MAIN request ', req.url); + + app.use(express.static('dist' + '/public')); }); app.listen(port, () => {