0

I have been trying with the following sample program?

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 ("Connected..");
    var path = url.parse (request.url).pathname;
    console.log ("path is " + path);

    switch (path) {
     case '/':
        response.write ("Hello.. use connectme.html ");
        break;

     case '/test':

        console.log ("Hello insideo /test//");
        response.write ("Hello. test..");
        break;

    case '/sock.html':
        console.log ("sock.html..");
        response.writeHead (200, {'Content-Type': 'text/html'});
        fs.readFile(__dirname+path, function (error, data){
        console.log ("Data is .. " + data);
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write(data, 'utf8');
        });
        break;

     case '/index.html':

        response.write ("Inside connect.em.html");
        break;

     default:
        console.log (" Hell. i am not suppoed to be here...");
        response.writeHead(404);
        response.write("Unable to Find this page..");
    break;

   } // Switch  
   response.end();
 });

server.listen(8765);

In Particular,

the below line doesn't seem to sending the html file from server to client:

console.log ("Data is .. " + data);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data, 'utf8');

The HTML content is printed in the console. but in the browser nothing is displayed.

In sock.html, I just have one div with paragraph with dummy content.

I am sure i am doing some subtle error. Kindly Guide!!!.

1 Answer 1

1

You're ending the response before waiting for the readFile callback to execute. Writing to a WritableStream after it has been ended is an error. Remove the response.end call at the end and replace the calls to response.write with response.end(data, encoding);

case '/sock.html':
        console.log ("sock.html..");
        response.writeHead (200, {'Content-Type': 'text/html'});
        fs.readFile(__dirname+path, function (error, data){
          console.log ("Data is .. " + data);
          response.writeHead(200, {'Content-Type': 'text/html'});
          response.end(data, 'utf8');
        });
        break;

Also, the call to url.parse(request.url).pathname is redundant since request.url already refers to the requested path (the path in the status line).

Sign up to request clarification or add additional context in comments.

3 Comments

I tried, replacing response.write (data ) to response.end(data, 'utf8'), but still i do not see the html displayed in the browser.
Ha.. Looks like i need to remove response.end() at the end of this function, and introduce response.end() to each switch cases, and then it works. BTW, is it the right way to do it?
Using a framework such as Express or koa would be better since they handle URL routing and HTTP verbs among other things. Your application doesn't handle HTTP verbs and the switch case block may become very large after adding more routes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.