Nikhil Raman
Nikhil Raman

Reputation: 7

Node.js HTTP WebServer Write after End error

I am new to node.js and have been trying to setup just a basic server to start with.

I have the code below running and I get a "Write After End" error.

Socket.html is a basic html file that has a hello world string in the body. There is literally nothing else in that file. I have tried using the "ReadFileSync" method and that throws up a whole new set of errors that I don't fully understand.

I will appreciate any help on this. I am brand new to this so please go a little easy on me :) Thank you in advance!

I have verified that the path is correct, and that the buffer does have the data.

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.substr(1);
path = "\\" + path;

switch(path){
    case '/':
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write('hello world');
        break;
    case '\\socket.html':
        //console.log(path);
        fs.readFile(__dirname + path, function(error, data){
            if(error){
                response.writeHead(404);
                response.write("This domain is missing");
            }
            else{
                console.log(data);
                response.writeHead(200, {"Content-Type": "text/html"});
                response.write(data,"utf8");
            }
        });
        break;
    default:
        response.writeHead(404);
        response.write("This domain is missing");
        break;
}

response.end();
});

server.listen(8001);

Upvotes: 0

Views: 7689

Answers (1)

Luca Schimweg
Luca Schimweg

Reputation: 797

I think when you read from the FileSystem async, the response.end() method is called before response.write() I would suggest using the following code instead:

var http = require('http'); 
var url = require('url'); 
var fs = require('fs'); 
var server = http.createServer(function(request, response) {
    var path = url.parse(request.url).pathname.substr(1);
    path = "\\" + path; 
    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("This domain is missing"); 
                } else {
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8"); 
                } 
                response.end();
            }); 
        break; 
        default: 
            response.writeHead(404); 
            response.write("This domain is missing"); 
            response.end();
        break;
    } 
});

Upvotes: 2

Related Questions