如果我啓動服務器並轉到localhost:4000,服務器會發出index.html。但是index.html不會加載它的style.css,也不會連接到服務器。但是,如果我雙擊本地的index.html文檔,它將連接到服務器。我的迴應有什麼問題?node.js - socket.io 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(){...}
謝謝凱文,我第一次有一個不同的environemnt ... – poppel 2013-03-26 17:41:27