three.js script not working in node.js server

  backend, html, javascript, node.js, three.js

I’m new to web dev and im trying to run a threejs app on a nodejs server and the script main.js in the html won’t execute:

<body>
    <script src="../project/node_modules/three/build/three.js"></script>
    <script src="../project/main.js"></script>
</body>

This is my node code, the server itself works:

const http = require('http');
const fs = require("fs");
const port = 3000;

const server = http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type" : "text/html"});

    fs.readFile("index.html", function(error, data){
        if(error){
            res.writeHead(404);
            res.write("Error: file not found");
        }else{
            res.write(data);
        }
        res.end();
    });
    
});

server.listen(port, function (error) {
    if (error){ 
        console.log("Something went wrong", error);
    }else{
        console.log("Server listening on port: "+port);
    }
});

I checked the Network in DevTools and the server response is the html file.
I’ve been looking everywhere for a few hours and I can’t find a solution, I’d be happy if you can help me out, thanks.

Source: Ask Javascript Questions

LEAVE A COMMENT