我是Node.js和Socket.io的新手.我已经构建了一个非常基本的Web服务器,但是在使用它时,我无法加载socket.io客户端文件(我得到了404).
我正在尝试使用此客户端代码:
<script src="/socket.io/socket.io.js"></script>
Run Code Online (Sandbox Code Playgroud)
我的理解是Node应该选择并解决它?它在一个更简单的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');
}
});
} …Run Code Online (Sandbox Code Playgroud)