/ / XMLHttpRequest non definito quando si utilizza JSONLoader in Node - javascript, node.js, three.js

XMLHttpRequest non definito quando si utilizza JSONLoader nel nodo: javascript, node.js, three.js

Sto scrivendo un gioco in Three.js e, come gioco multiplayer, ho bisogno di verificare le posizioni dei client sul lato server per evitare imbrogli. Attualmente sto provando a caricare un modello sul server, come tale:

var THREE = require("three");
var loader = new THREE.JSONLoader();
loader.load( "./models/tree.json", function ( geometry, materials ) {
var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
res.send(mesh);
});

Tuttavia, il server muore e sputa

var request = new XMLHttpRequest();
ReferenceError: XMLHttpRequest is not defined
at FileLoader.load

Questa richiesta sta arrivando a node_modulesthreebuildthree.js:29258, dove un XMLHttpRequest è fatto.

Perché sta succedendo? Sto facendo qualcosa di sbagliato o questa parte di Three.js è rotta per Node?

risposte:

7 per risposta № 1

Three.js utilizza un XMLHttpRequest per caricare file come il tuo file JSON. XMLHttpRequest è integrato negli ambienti dei browser, ma non è incorporato in un ambiente Node, quindi non è definito, quindi l'errore. Dovrai installare il xmlhttprequest pacchetto tramite NPM per usarlo con Node.

Poiché Three.js non richiede il file xmlhttprequest modulo, dovrai impostare una variabile globale in modo che new XMLHttpRequest funzionerà:

global.XMLHttpRequest = require("xmlhttprequest");