Fix node-http.js

This commit is contained in:
Siklos 2022-08-05 11:56:45 +02:00
parent d1e04769bb
commit 35bb95f206

View file

@ -2,27 +2,39 @@ import http from 'http';
const host = 'localhost'; const host = 'localhost';
const port = 5000; 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') { if (request.method === 'POST') {
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Content-Type', 'application/json'); response.setHeader('Content-Type', 'application/json');
const url = request.url; const url = request.url;
let json; let json;
if (url === '/GetSVGLayoutConfiguration') { if (url === '/ApplicationState') {
json = GetSVGLayoutConfiguration(); const buffers = [];
} else if (url === '/FillHoleWithChassis') { for await (const chunk of request) {
json = FillHoleWithChassis(request); buffers.push(chunk);
} else if (url === '/SplitRemplissage') { }
json = SplitRemplissage(request); 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 { } else {
// TODO: Return 404 rather than this // TODO: Return 404 rather than this
json = GetSVGLayoutConfiguration(); json = GetSVGLayoutConfiguration();
} }
response.writeHead(200); response.writeHead(200);
return response.end(JSON.stringify(json)); return response.end(JSON.stringify(json));
} else if (request.method === 'OPTIONS') { } 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); response.writeHead(200);
return response.end(); return response.end();
} }
@ -269,18 +281,18 @@ const GetSVGLayoutConfiguration = () => {
const FillHoleWithChassis = (request) => { const FillHoleWithChassis = (request) => {
const maxWidthChassis = 3000; const maxWidthChassis = 3000;
const nbChassis = Math.ceil(request.ContainerActions.Width / maxWidthChassis); const nbChassis = Math.ceil(request.ContainerAction.Width / maxWidthChassis);
const lstModels = []; const lstModels = [];
for (let i = 0; i <= nbChassis; i++) { for (let i = 0; i <= nbChassis; i++) {
if (i === 1 && request.ContainerAction.ExistOnBefore) { 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) { if (i < nbChassis) {
lstModels.Add({ Type: 'Dilatation' }); lstModels.push({ Type: 'Dilatation' });
} }
if (i === nbChassis && request.ContainerAction.ExistOnAfter) { if (i === nbChassis && request.ContainerAction.ExistOnAfter) {
lstModels.Add({ Type: 'Dilatation' }); lstModels.push({ Type: 'Dilatation' });
} }
} }
return { return {