当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Nodejs sending basic webpage

问题描述:

I'm following this tutorial to learn about Nodejs and Io socket.

But i'm having a problem with loading the page socket.html.

My goal is to let him print this:

<html>

<head></head>

<body>This is our socket.html file</body>

</html>

But the result is an empty page.

I tried to let him print different things as you can see in commentary in my code but nothing seems to work. I'm sure the code is reaching the right loop because he prints "enterLoop" in my console as expected.

Why is this not working?

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){

fs.readFile(__dirname + "/socket.html", 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.writeHead(404);

//response.write("opps this doesn't exist - 4055");

//response.writeHead(200, {"Content-Type": "text/html"});

//response.write(data);

//response.writeHead(200, {'Content-Type': 'text/html'});

//response.write('hello world');

console.log("enterLoop");

}

});

break;

default:

response.writeHead(404);

response.write("opps this doesn't exist - 404");

break;

}

response.end();

});

server.listen(8001);

io.listen(server);

网友答案:

You're reading the file in asynchronously, so response.end() is getting called before it can read the file and write the response. Move the response.end() to within the callback and it should work.

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){
                fs.readFile(__dirname + "/socket.html", function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                    response.end();
                }
                else{

                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                    //response.writeHead(404);
                    //response.write("opps this doesn't exist - 4055");
                    //response.writeHead(200, {"Content-Type": "text/html"});
                    //response.write(data); 
                    //response.writeHead(200, {'Content-Type': 'text/html'});
                    //response.write('hello world'); 
                    console.log("enterLoop");
                    response.end();
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            response.end();
            break;
    }

});

server.listen(8001);

io.listen(server);
分享给朋友:
您可能感兴趣的文章:
随机阅读: