How do I call loader function inside app.post in Node.js?

I am trying to create a node app in which I need to calculate the volume of the stl file which is uploaded by the user. I am using express and multer to handle the file uploading process. I am using three.js to calculate the volume of the stl file. However, when I add the volume code inside app.get(), it doesn’t work. I have tried the code on plain js and it works perfectly and outputs the volume. I am not sure if the code gets executed when running on node as nothing gets printed in the terminal console. I need this code to work on the server side as I don’t want the client to see this code and manipulate it. Please help me with this. Here is my current code:

app.get(’/’, function (req, res) {
console.log(“Hello!”); //This line gets printed
res.render(‘pages/index’);
});

app.post(’/upload’, upload.single(‘stl_file’), (req, res) => { //stl_file is the name attribute of input tag in html
var loader = new STLLoader();
loader.load(req.file, function (geometry) {
Code to calculate volume
console.log("stl volume is " + volume); //This line does not get printed
Code to calculate size
let stlSize = size;
console.log(stlSize); //This line does not get printed
});
return res.json({ status: ‘OK’});
});

app.listen(3001);

i don’t think you can use loaders in node because they rely on web apis like XHRRequests. but you could get lucky by trying to parse the model instead. some loaders parse functions work in node: three.js/STLLoader.js at 6b1cf0a5478d0844ae0062d74535adc25e65c385 · mrdoob/three.js · GitHub

for this to work you need the raw arraybuffer and then feed it to it:

new STLLoader().parse(buffer)

ps. i haven’t tried it, but if you run into problems ragarding DataView and node array buffers, you can most likely convert them. i’ve found this issue for instance: javascript - Using DataView with nodejs Buffer - Stack Overflow

1 Like

@drcmda thanks for the answer. Could me help me with the code? I am new to three.js and still cannot figure out what exactly needs to be done. Thank you.

i did, the code above parses an existing arraybuffer. Loader().load fetches a file first via xhrrequest and then parses. so by using parse you avoid the fetching. you can load a file as an arraybuffer using the node api.

I am currently trying to parse the file using this code but am getting a error:

"RangeError: Offset is outside the bounds of the DataView"

var enc = new TextEncoder();
    var arrayBuffer = enc.encode(req.file).buffer;
    console.log(arrayBuffer);
    var loader = new STLLoader();
    loader.parse(arrayBuffer, function (geometry) {
            Code to calculate volume
console.log("stl volume is " + volume); //This line does not get printed
Code to calculate size
let stlSize = size;
console.log(stlSize); //This line does not get printed
});
return res.json({ status: ‘OK’});
});
    });

can’t help you with that. if stl is binary you probably don’t need textencoder. i think i’d just try fs.readFileSync(myFile, null).buffer;