Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between readFile() and readFileSync()

The following code outputs the content of the index.html (it just contains the text hello world) to the browser. However, when I replace readFile() with readFileSync(), the request times out.

What am I missing? Is a different kind of buffer required? I am using node 0.61 and express 2.4.

var express = require('express'); var fs = require('fs');  var app = express.createServer(express.logger());  app.get('/', function(request, response) {     fs.readFile('index.html', function(err, data){         response.send(data.toString());     }); });  var port = process.env.PORT || 5000; app.listen(port, function() {   console.log("Listening on " + port); }); 
like image 367
Ali Avatar asked Jul 11 '13 22:07

Ali


People also ask

What is difference between readFileSync and readFile?

In fs. readFile() method, we can read a file in a non-blocking asynchronous way, but in fs. readFileSync() method, we can read files in a synchronous way, i.e. we are telling node. js to block other parallel process and do the current file reading process.

What is the use of readFile method?

The readFile method reads the file indicated by the passed file path, and returns its contents. The path is internally converted by osSafeSpec() . The method assumes that the character of the file are in the default encoding. (The default encoding is set by the Java VM.)

What Is syntax of readFile () method?

Syntax: fsPromises.readFile( path, options ) Parameters: The method accept two parameters as mentioned above and described below: path: It holds the name of the file to read or the entire path if stored at other location. It is a string, buffer, URL or a filename. options: It holds the encoding of file.


2 Answers

fs.readFile takes a call back which calls response.send as you have shown - good. If you simply replace that with fs.readFileSync, you need to be aware it does not take a callback so your callback which calls response.send will never get called and therefore the response will never end and it will timeout.

You need to show your readFileSync code if you're not simply replacing readFile with readFileSync.

Also, just so you're aware, you should never call readFileSync in a node express/webserver since it will tie up the single thread loop while I/O is performed. You want the node loop to process other requests until the I/O completes and your callback handling code can run.

like image 184
bryanmac Avatar answered Oct 05 '22 12:10

bryanmac


'use strict' var fs = require("fs");  /***  * implementation of readFileSync  */ var data = fs.readFileSync('input.txt'); console.log(data.toString()); console.log("Program Ended");  /***  * implementation of readFile   */ fs.readFile('input.txt', function (err, data) {     if (err) return console.error(err);    console.log(data.toString()); });  console.log("Program Ended"); 

For better understanding run the above code and compare the results..

like image 20
Saurabh Chauhan Avatar answered Oct 05 '22 11:10

Saurabh Chauhan