【问题标题】:Node.js returns a blank html file to clientNode.js 向客户端返回一个空白的 html 文件
【发布时间】:2015-06-20 02:15:04
【问题描述】:

我是 node.js 的新手,并且找到了一个简单的教程来设置一个简单的 node.js 服务器。我正在尝试在客户端呈现一个 html 页面,我确实得到了该文件,我可以在控制台中看到该文件,但浏览器显示一个空页面?这是我的 server.js 文件:

var http = require('http'); 
var url = require('url');                                                       
var fs = require('fs');                                 

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("Oops, this doesn't exist! - 404");          
            }
            else {                                                          
                response.writeHead(200, {"Content-Type": "text/html"});     
                response.write(data, "utf8");
                console.log('Connection working!');
            }
        });
        break;
    default:
        response.writeHead(404);
        response.write("Oops, this doesn't exist - 404");                   
        break;
}
    response.end();                                                             
});

server.listen(8001);

这是我的简单 html 文件 socket.html

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <div>
            <h1>This is the socket.html file</h1>
        </div>
    </body>
</html>

我在浏览器中的回复:

我仔细检查了我使用的“url”和“fs”模块是否正确,我认为我这样做了吗?有人可以帮忙吗?

【问题讨论】:

    标签: html node.js client-server client server


    【解决方案1】:

    fs.readfile 是异步的,将在您调用response.end() 后返回文件内容。

    这是一个快速修复。希望它有效。 我将response.end() 移动到fs.readFile 回调中。

    var http = require('http');
    var url = require('url');
    var fs = require('fs');
    
    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("Oops, this doesn't exist! - 404");
                    }
                    else {
                        response.writeHead(200, {"Content-Type": "text/html"});
                        response.write(data, "utf8");
                        console.log('Connection working!');
                    }
                    response.end();
                });
                break;
            default:
                response.writeHead(404);
                response.write("Oops, this doesn't exist - 404");
                response.end();
                break;
        }
    });
    
    server.listen(8001);
    

    【讨论】:

    • 移动 'response.end()' 在 console.log 中给我这个错误:'GET localhost:8001/socket.html net::ERR_CONNECTION_REFUSED'
    • 这不应该是这个原因。你是说你在服务器的控制台中得到它吗?
    • 我将response.end() 移至默认值,现在它可以工作了!但从逻辑上讲,我不明白为什么。如果我的第一个“案例”是真的,我还需要response.end()吗?
    • response.end 是必需的,除非通过例如管道流到response 来调用它。但是您没有这样做,因此您需要致电response.end。在上面的“修复”中,我将response.end 移动到所有案例中,而不仅仅是default 案例。你这样做了吗?
    • 好。但是,我建议使用某种路由器,例如express。 Express 甚至可以为您提供静态文件out of the box
    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 2014-03-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多