0

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

1 Answer 1

2

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;
    } 
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Luca! That fixed the problem I was having! I had a feeling it had to do with the async Fs.read, but since I'm new I wasn't sure how to deal with it.
I'm happy that I could help you :)
Hey @Luca ! I have run into another node problem. I am learning the ropes and I am not entirely sure why I am getting some errors. I have posted a new question at: stackoverflow.com/questions/31506271/… Could you maybe take a look and help me out there? Thanks in advance!

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.