Javascript three.js脚本在node.js服务器中不工作
我是web开发新手,正在尝试在nodejs服务器上运行threejs应用程序,html中的脚本main.js将不会执行:Javascript three.js脚本在node.js服务器中不工作,javascript,html,node.js,three.js,backend,Javascript,Html,Node.js,Three.js,Backend,我是web开发新手,正在尝试在nodejs服务器上运行threejs应用程序,html中的脚本main.js将不会执行: <body> <script src="../project/node_modules/three/build/three.js"></script> <script src="../project/main.js"></script> </body>
<body>
<script src="../project/node_modules/three/build/three.js"></script>
<script src="../project/main.js"></script>
</body>
我在DevTools中检查了网络,服务器响应是html文件。
我到处找了几个小时,找不到解决方案,如果你能帮我,我会很高兴的,谢谢。你必须使用
app.use(express.static('public'))将文件夹公开为静态文件容器。
其中public是文件夹名:。然后将index.html和所有JS文件移到这个文件夹中,而不是为此编写一个HTTP请求处理程序。我想我没有弄错,但我没有使用express:)@Guerric POh是的,我读得太快了。对不起。您不使用express有什么特别的原因吗<代码>http的级别非常低。如果继续使用http
,则必须创建一个URL,将JS文件提供给web页面,就像对index.html
所做的那样。Express.js有助于将URL映射到处理程序,而http
则没有,因此您必须根据req
对象编写自己的逻辑。我会试着用express做这件事。看起来容易些,谢谢@Guerric P
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);
}
});