Javascript can';t在node.js服务器上显示html文件
我有一个简单的带有node.js和socket.io的服务器和客户端代码。当我在我的计算机localhost:8001/index.html上运行时,我收到一条消息,说找不到我的html文件。有人能帮我解决这个问题吗?我已经查找了一些参考资料,但仍然无法解决这个问题。 这是我的server.js和index.html文件 server.jsJavascript can';t在node.js服务器上显示html文件,javascript,html,node.js,socket.io,Javascript,Html,Node.js,Socket.io,我有一个简单的带有node.js和socket.io的服务器和客户端代码。当我在我的计算机localhost:8001/index.html上运行时,我收到一条消息,说找不到我的html文件。有人能帮我解决这个问题吗?我已经查找了一些参考资料,但仍然无法解决这个问题。 这是我的server.js和index.html文件 server.js var http = require("http"); var url = require('url'); var fs = require('fs');
var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello worlds');
response.end();
break;
case 'index.html':
fs.readFile('./index.html', function(error, data){
if (error){
response.writeHead(404);
response.write("oppsa this doesn't exist - 404");
response.end();
console.log(error);
}
else{
console.log('hi!');
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
response.end();
}
});
break;
default:
response.writeHead(404);
response.write("This page does not exist - 404");
response.end();
console.log(path);
break;
}
});
server.listen(8001);
io.listen(server);
index.html
<!doctype html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script> var io = io.connect();</script>
<div>This is our socket.html file</div>
</body>
</html>
var io=io.connect();
这是我们的socket.html文件
Wilson:case'/index.html'中是否缺少“/”:?Aravind给出了正确的答案。此外,请参阅以下链接,其中介绍了您将在“that”教程中发现的所有错误: