2

I would like to know how are require('/file.json'), reafFile('/file.json', callback) and readFileSync('/file.json', 'utf-8') different from each other, and also when to use each.

The reason why I want to know this is because I want to send a file from node.js to angular.js, I've used the three ways and i've noticed that require parses the file, is the shortest way to get done what i want, in both readFile and readFileSync i have to parse the file.

A) require('/file.json')

To use this way we only have to do the following:

var client_content = require( './client/content.json' );

app.get( '/api/v1/content', function ( request, response ) {

    response.setHeader( 'Content-Type', 'application/json' );
    response.json( client_content );

} );

B) reafFile('/file.json', callback)

In this case we have to parse the file

app.get( '/api/v1/content', function ( request, response ) {

    response.setHeader( 'Content-Type', 'application/json' );
    readJSONFile( './client/content.json', function ( err, json ) {
        if ( err ) {
            throw err;
        }
        return response.json( json );
    } );

} );

function readJSONFile( filename, callback ) {
    require( "fs" ).readFile( filename, function ( err, data ) {
        if ( err ) {
            callback( err );
            return;
        }
        try {

            callback( null, JSON.parse( data ) );

        } catch ( exception ) {
            callback( exception );
        }
    } );
}

C) readFileSync('/file.json', 'utf-8')

We have to parse the file too

app.get( '/content', function ( request, response ) {
    response.json(JSON.parse(require('fs').readFileSync('./client/content.json', 'utf8')));
}

So, what way is better and when is better to use each of them?, what are the differences between them?

Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131

1 Answers1

1

To answer your question about async and the difference between readFile and readFileSync (it looks like require is taken care of in the above comment):

readFile is an asynchronous function, meaning node will kick off the function, and then continue to process other things in your app while that function is waiting to return some data (in your case the contents of content.json). Once content.json has been read into memory, a callback function will be executed and passed two arguments. The first an error if one occurred, the second argument the json data from your file. You can then handle that data within the callback function however you would like.

readFileSync on the other hand will hold up (block) your application from processing anything else until the data has been read from content.json. This can take a long time depending on the size of content.json and many people would not want to hold up their entire application while this process is taking place. Hence the advantage to using nodeJs asynchronously.

The above is a simplification on how nodeJs works. For details about the event loop / event queue I would suggest watching this video. Event Loop in nodeJs

user2263572
  • 5,435
  • 5
  • 35
  • 57