*** Using THREE.js to create a 3D viewer space and loading gltf models into the viewer space via node.js express.
*** Currently, when accessing the default address http://localhost:port/, it displays the HTML,
app.get('/', (req, res) => {
res.sendFile(__dirname + '/mythreeviewer.html');
});
*** The following code sends a request for the gltf file to be loaded at a specific url.
async function getModelData() {
try {
const response = await fetch('http://localhost:port/getModel');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
}
*** Server-side response handling for the request is as follows.
app.get('/getModel', (req, res) => {
const directoryPath = "D:/Temp/modelA.gltf";
try {
// Navigate to a specific path and retrieve the model file.
}
catch (error){
console.error('Error handling request:', error);
return res.status(500).json({ error: 'Error handling request' });
}
});
What I want to achieve going forward is this.
When a user accesses http://localhost:port/model/AAA
(or http://localhost:port/model/BBB, CCC, DDD …)
The server should display the default HTML and at the same time read the gltf file within the “D:/Temp/model/AAA/” path.
In other words, the server-side should handle the value passed as a URL parameter to load the model.
However, I also need to provide the HTML. Please advise on how to do this.