我对Node.js和Socket.io很陌生。我已经构建了一个非常基本的Web服务器,但是在使用它时,我无法加载socket.io客户端文件(我得到一个404)。Node.js - Socket.io客户端文件没有被基本节点Web服务器服务
我试图用这个客户端代码:
<script src="/socket.io/socket.io.js"></script>
我的理解是,节点应该选择这并解决它?它在一个更简单的Web服务器示例上执行。
我的web服务器例子,其中尚未解决如下:
var server = http.createServer(function (request, response) {
console.log('request starting...');
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
else {
response.writeHead(404);
response.end();
}
});
我如果它确实解决Web服务器的代码如下:
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
任何人都可以是什么原因造成建议它不使用第一个Web服务器示例为客户端上的socket.io文件提供服务?
最好的问候,本。
哪里你的socket.io代码是? – Raynos