From 35bb95f206c51ff017d0173ccfb7499793c418d8 Mon Sep 17 00:00:00 2001 From: Siklos Date: Fri, 5 Aug 2022 11:56:45 +0200 Subject: [PATCH] Fix node-http.js --- test-server/node-http.js | 44 +++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/test-server/node-http.js b/test-server/node-http.js index b78fe50..e6c0d74 100644 --- a/test-server/node-http.js +++ b/test-server/node-http.js @@ -2,27 +2,39 @@ import http from 'http'; const host = 'localhost'; const port = 5000; -const requestListener = function(request, response) { +const requestListener = async(request, response) => { + response.setHeader('Access-Control-Allow-Origin', '*'); + response.setHeader('Access-Control-Allow-Headers', '*'); + response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); if (request.method === 'POST') { - response.setHeader('Access-Control-Allow-Origin', '*'); response.setHeader('Content-Type', 'application/json'); const url = request.url; let json; - if (url === '/GetSVGLayoutConfiguration') { - json = GetSVGLayoutConfiguration(); - } else if (url === '/FillHoleWithChassis') { - json = FillHoleWithChassis(request); - } else if (url === '/SplitRemplissage') { - json = SplitRemplissage(request); + if (url === '/ApplicationState') { + const buffers = []; + for await (const chunk of request) { + buffers.push(chunk); + } + const data = Buffer.concat(buffers).toString(); + const bodyParsed = JSON.parse(data); + console.log(bodyParsed); + switch (bodyParsed.Action) { + case 'FillHoleWithChassis': + json = FillHoleWithChassis(bodyParsed); + break; + case 'SplitRemplissage': + json = SplitRemplissage(bodyParsed); + break; + default: + break; + } } else { - // TODO: Return 404 rather than this + // TODO: Return 404 rather than this json = GetSVGLayoutConfiguration(); } response.writeHead(200); return response.end(JSON.stringify(json)); } else if (request.method === 'OPTIONS') { - response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); - response.setHeader('Access-Control-Allow-Headers', '*'); response.writeHead(200); return response.end(); } @@ -269,18 +281,18 @@ const GetSVGLayoutConfiguration = () => { const FillHoleWithChassis = (request) => { const maxWidthChassis = 3000; - const nbChassis = Math.ceil(request.ContainerActions.Width / maxWidthChassis); + const nbChassis = Math.ceil(request.ContainerAction.Width / maxWidthChassis); const lstModels = []; for (let i = 0; i <= nbChassis; i++) { if (i === 1 && request.ContainerAction.ExistOnBefore) { - lstModels.Add({ Type: 'Dilatation' }); + lstModels.push({ Type: 'Dilatation' }); } - lstModels.Add({ Type: 'Chassis' }); + lstModels.push({ Type: 'Chassis' }); if (i < nbChassis) { - lstModels.Add({ Type: 'Dilatation' }); + lstModels.push({ Type: 'Dilatation' }); } if (i === nbChassis && request.ContainerAction.ExistOnAfter) { - lstModels.Add({ Type: 'Dilatation' }); + lstModels.push({ Type: 'Dilatation' }); } } return {