Node.js with fs.readFile cannot display file contents via http

I am learning node.js, but struggling with a function fs.readFilein my simple web server code (below demo.js). If I get access to http://localhost:8001/demo.html, I don't get any data in the browser, but if I get access to "/" or any other file name, I see "hello world" or 404 pages as expected.

I included the contents of demo.html and the output of the node console, where the log messages show that readFile is reading the file in order, but I do not receive anything in the browser and do not see the “Got here D” log message.

I am sure that I should miss something obvious, but I can not understand that. Any help would be appreciated!

Code demo.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 '/demo.html':
            fs.readFile(__dirname + path, function(error,data){
                console.log("error: " + error);
                console.log("data: " + data);
                if (error){
                    console.log("Got here A");
                    response.writeHead(404);
                    response.write("oops problem loading this page - 404");
                }   
                else{
                    console.log("Got here B");
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data);
                }
            });
            break;
        default:
            console.log("Got here C");
            response.writeHead(404);
            response.write("oops this page doesn't exist - 404");
            break;
    }
    response.end();
    console.log("Got here D");
});

server.listen(8001);

demo.html:

<html>
<body>This is my demo page</body>
</html>

node console output:

C:\code\Node>node demo.js
Connection
Got here D
error: null
data: <html>
<body>This is my demo page</body>
</html>

Got here B
+4
1

response.end().

fs.readFile - ; node, I/O. , , , .

Node fs.readFile, response.end() , .

, response.end() :

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 '/demo.html':
            fs.readFile(__dirname + path, function(error,data){
                console.log("error: " + error);
                console.log("data: " + data);
                if (error){
                    response.writeHead(404);
                    response.write("oops problem loading this page - 404");
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data);
                }

                response.end();
            });
            break;
        default:
            response.writeHead(404);
            response.write("oops this page doesn't exist - 404");
            response.end();
            break;
    }
});
+9

Source: https://habr.com/ru/post/1540714/


All Articles