Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
node.js-socket.io index.html不响应_Node.js_Socket.io_Response - Fatal编程技术网

node.js-socket.io index.html不响应

node.js-socket.io index.html不响应,node.js,socket.io,response,Node.js,Socket.io,Response,如果启动服务器并转到localhost:4000,服务器将给出index.html。但是index.html不会加载其style.css,也不会连接到服务器。但是如果我双击local index.html文档,它就会连接到服务器。我的回答有什么不对 var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(4000

如果启动服务器并转到localhost:4000,服务器将给出index.html。但是index.html不会加载其style.css,也不会连接到服务器。但是如果我双击local index.html文档,它就会连接到服务器。我的回答有什么不对

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')

app.listen(4000);


function handler (req, res) {
    fs.readFile(__dirname + '/public/index.html',
 function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {...}
index.html

<!DOCTYPE HTML>
<html>
  <head>

    <link rel="stylesheet",type="text/css" href="styles.css"/>
    <script type="text/javascript" src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js">></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

  </head>
  <body>

<script>
var socket = io.connect('http://localhost:4000');

socket.on('connect', function(){...}

>
var socket=io.connect('http://localhost:4000');
socket.on('connect',function(){…}

您是否尝试过像这样引用socket.io.js


在处理函数中,您似乎通过向用户返回index.html来响应所有请求。这就是为什么不返回CSS样式表和Javascript文件的原因