如何查看socket.html的输出?

时间:2014-05-02 12:29:23

标签: node.js socket.io

我是node.js的新手。 我正在尝试实施danielnill example教程

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 world');
            break;
        case 'socket.html':
            fs.readFile(__dirname + path, function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            break;
    }
    response.end();
});

server.listen(8001);

io.listen(server);

socket.html

<html>
  <head>
    <script src="/socket.io/socket.io.js"></script>
  </head>
  <body>
    <script>
      var socket = io.connect();
    </script>
    <div>This is our socket.html file</div>
  </body>
</html>

当我尝试从浏览器运行此http://localhost:8001/socket.html网址时。它是默认情况,而不是'socket.html'案例。

请帮助我在这个例子中执行'socket.html'案例。

1 个答案:

答案 0 :(得分:4)

您发布的内容有两个问题:

  1. 请求网址path时,/socket.html变量等于http://localhost:8001/socket.html,而不是socket.html;你需要相应地更新案例陈述。

  2. fs.readFile回调将无法将回复写回客户端(浏览器),因为response.end();已被调用;您需要将response.end()移到每个case语句中。

  3. 以下是更新后的代码:

    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 world');
                response.end();
                break;
            case '/socket.html':
                fs.readFile(__dirname + path, function(error, data){
                    if (error){
                        response.writeHead(404);
                        response.write("opps this doesn't exist - 404");
                    }
                    else{
                        response.writeHead(200, {"Content-Type": "text/html"});
                        response.write(data, "utf8");
                    }
                    response.end();
                });
                break;
            default:
                response.writeHead(404);
                response.write("opps this doesn't exist - 404");
                response.end();
                break;
        }
    });
    
    server.listen(8001);
    
    io.listen(server);