Aquarius_Girl
Aquarius_Girl

Reputation: 22946

How to read and open two different files in nodejs?

I have observed in this code that we can't open the file after we have readFile.

The computer complains about Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Of course if I remove the readFile function then this error goes away but I want to understand why this error is occurring even though I am using two different files for reading and opening.

What is the way to read and open two different files?

var objHttp = require('http');
var objFS = require('fs');

objHttp.createServer(function(argClientRequest, argResult) {
    objFS.readFile('z.html',
        function(argError, argData) {
            argResult.writeHead(200, {
                'Content-Type': 'text/html'
            });
            argResult.write(argData);
            argResult.end();
        }
    );
    objFS.open('mynewfile1.txt', 'r', (argErr, argFD) => {
        if (argErr) throw argErr;

        objFS.readFile('mynewfile1.txt',
            function(argError, argData) {
                if (argError) throw argError;

                argResult.writeHead(200, {
                    'Content-Type': 'text/html'
                });
                argResult.write(argData);
                return argResult.end();
            }
        );


        objFS.close(argFD, (argErr) => {
            if (argErr) throw argErr;

        });
    });
}).listen(8080);

Upvotes: 1

Views: 71

Answers (4)

Manav Mandal
Manav Mandal

Reputation: 325

var objHttp = require('http');
var objFS = require('fs');
function firstReader(argResult){

    return new Promise(function(resolve,reject){
        objFS.readFile("z.html",
        function(argError, argData) {
            argResult.writeHead(200, {
                'Content-Type': 'text/html'
            });
            argResult.write(argData);
         
        }
    );
    })
   
}
function secondReader(argResult){
    return new Promise(function(resolve,reject){
        objFS.open('mynewfile1.txt', 'r', (argErr, argFD) => {
            if (argErr) throw argErr;
    
            objFS.readFile('mynewfile1.txt',
                function(argError, argData) {
                    if (argError) reject();
    
                    argResult.writeHead(200, {
                        'Content-Type': 'text/html'
                    });
                    argResult.write(argData);
                }
            );
    
    
            objFS.close(argFD, (argErr) => {
                if (argErr) reject();
    
            });
        });
    })
}
objHttp.createServer(function(argClientRequest, argResult) {
   Promise.all([firstReader(argResult),secondReader(argResult)]).then(function(){
    argResult.end();
   }).catch(function(){
    argResult.end();
   })
   
}).listen(8080);

Upvotes: 1

Manav Mandal
Manav Mandal

Reputation: 325

you are using argResult.end(); twice. You cannot send response twice. For this problem you need to use promise the read the file and resolve them after callback. You can use

Promise.All([promise1,promise2]).then((data)=>{
argResult.end();
}) 

here promise1 is first file reader and promise2 is second file reader

and then send the response back when both the promise are resolved

Upvotes: 1

Shihab
Shihab

Reputation: 2679

argResult is a stream. In the first objFS.readFile call you are ending the argResult stream and then again you are trying to write to the argResult stream in the second objFS.readFile call. That is why you are getting the error.

The problem is not with file access. If you remove the http listener and run the code then you will see no error in the console.

Upvotes: 1

Surya
Surya

Reputation: 11

The problem is argResult.end(); This gets returned immediately after reading z.html and when mynewfile1.txt is read, the headers and already sent to the client. You are trying to send it again which is technically not possible. You will have to send the responses only once.

Upvotes: 1

Related Questions