1

I want simulate client - server node.

Here is my 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("opps this doesn't exist - 404 1");

                }
                */

                    response.writeHead(200, {'Content-Type': 'text/html'});
                   // response.write("data terbaca 2"); 
                    response.write(data, "utf8");

            }); 
            response.writeHead(200, {'Content-Type': 'text/html'}); 
            response.write('hello world2 + ' + __dirname + ' ' + path);

            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404 3");
            break;
    }
    response.end(); 
}); 

server.listen(8000); 

and here is my client html

<html>
<head></head>
<body>Ini Socket html lohhh</body>
</html>

My directory strucure is in D:/cobaa 1. server.js 2. socket.html

I node the server, and access the localhost:8000/socket.html and get

hello world2 + D:\cobaa /socket.html

can anybody get me the solution,please? Big thanks :)

pegasustech
  • 67
  • 4
  • 13

1 Answers1

1

The catch here is you are finishing the response before you actually write the file contents. The fs.readFile is asynchronous, so the writing of the file contents is deferred until the read completes, so it reaches response.end() first and executes it.

The response should only be sent after file has been read. So put all your response handling inside the callback

        fs.readFile(__dirname + path, function(error, data){

            /*  if (error){
                response.writeHead(404);
                response.write("opps this doesn't exist - 404 1");
                }
            */
                response.writeHead(200, {'Content-Type': 'text/html'});
                response.write(data, "utf8");
                response.write('hello world2 + ' + __dirname + ' ' + path);
                response.end();
        }); 
user568109
  • 47,225
  • 17
  • 99
  • 123
  • should those response.write calls and respond.end be async chained with callbacks, or does respond internally push those to a list to be iterated on end? I ask because usually I see only a single response.send call, which perhaps provides all of this. – Paul Jul 19 '13 at 15:33
  • 1
    @Paul They don't need callbacks. response being a stream `response.write`s are peformed in the order they are given (they use Eventemitters, write events are queued). `response.end` should be executed only once, which sends the response. Doing res.send(x) is equivalent to first res.write(x) then res.end() – user568109 Jul 19 '13 at 16:05
  • wow, sorry I came from blocking and syncronous technology. With nodeJS asynchronous feature, still bias to me. Thanks for the reminder :) – pegasustech Jul 21 '13 at 13:59
  • I have moved the response.end. I get blank every time I access the socket.html. When I check the page source, it still black too. – pegasustech Jul 22 '13 at 08:25