Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript can';t在node.js服务器上显示html文件_Javascript_Html_Node.js_Socket.io - Fatal编程技术网

Javascript can';t在node.js服务器上显示html文件

Javascript 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');

我有一个简单的带有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 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”教程中发现的所有错误: