简体   繁体   中英

In node.js Why should I use readFileSync for ssl cert and key? What are the pitfalls for using readFile?

var privateKey  = fs.readFileSync('/path/to/franciskim.co.key', 'utf8');
var certificate = fs.readFileSync('/path/to/franciskim.co.crt', 'utf8');
var credentials = { key: privateKey, cert: certificate }; 

Why not fs.readFile only? why readFileSync?

Because you usually do it once on the server startup and this is the only time when it's fine to use blocking calls like fs.readFileSync() or require() - on the first tick of the event loop.

You typically don't want anything else to happen before you read those keys and it is much easier to do with blocking calls. At that point no events are handled yet so no performance degradation and concurrency problems will happen.

But after the first tick of the event loop is over, you should only use non-blocking calls.

It's not that it's impossible to use fs.readFile() to get the cert files but that you would need to start anything else only after all of the callbacks of all of the relevant fs.readFile() calls has already been finished with success.

The primary reason is the complexity of examples. Using readFileSync lets you write synchronous sequential code that when giving an example negates the need to have to offer up a large variety of solutions one might provide for synchronizing asynchronous code.

For example, using readFile one might:

fs.readFile('/path/to/franciskim.co.key', 'utf8', function (err, privateKey) {
  if (err) { // .. handle no key found;
    return;
  }
  fs.readFileSync('/path/to/franciskim.co.crt', 'utf8', function (err, certificate) {
    if (err) { // .. handle no cert found;
      return;
    }
    var credentials = { key: privateKey, cert: certificate };
    // Now create https server here
  });
});

This example here is a close equivalent to readFileSync except now you have to handle errors explicitly. Using readFileSync will just cause an exception and the app to crash. With the async version you have to explicitly handle errors and often times different developers handles errors differently.

Using the async functions does open up opportunities to improve performance, for example by opening both files at the same time.

var async = require('async');

async.parallel([
  fs.readFile.bind(fs, '/path/to/franciskim.co.key', 'utf8'),
  fs.readFile.bind(fs  '/path/to/franciskim.co.key', 'utf8')
], function (err, results) {
  var credentials = { key: results[0], cert: results[1] };
  // Open https server here
});

Which is great and all, but now we are pulling in another library because there are multiple (bad) ways to have to implement this logic oneself.

The usage of readFileSync keeps examples on point with little room for interpretation and consistent error flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM