I’ll put an AD up front for my new Blog: Sangle’s Blog

This article was supposed to be written last November and now three months have passed… With V9.5 out of the way, here’s a summary of what’s changed in The latest version of Node.

There are two major releases of Node.js each year. In October 2017, Version 9.0 was released, and at the same time, Node.js 8.9.0 became the latest LTS release. This means that support for 8.9.0 will remain until the end of 2019, and one LTS release after that will be Node.js 10


View node.js release history

What’s new in Node.js 9?

http/2

Node 8.4 supports HTTP /2 for the first time

const http2 = require('http2');
const server = http2.createServer();
server.on('stream', (stream, requestHeaders) => {
  stream.respond({ ':status': 200, 'content-type': 'text/plain' });
  stream.write('hello ');
  stream.end('world');
});
server.listen(8000);
Copy the code

But because it’s still experimental, you need to add the — expose-http2 parameter to the runtime

$ node --expose-http2 h2server.js
Copy the code

In Node 9 this parameter is removed and can be used directly

$ node h2server.js
Copy the code

HTTP2 is enabled only when HTTPS is used in the browser

Other changes to HTTP /2 include:

  1. Added support for Alternative HTTP Services (ALTSVC)
  2. newmaxSessionMemory, which limits the maximum amount of memory allowed for a single HTTP2 thread. If this value is exceeded, http2 requests will be rejected
  3. Collect and report relevant informationHttp2SessionHttp2StreamBasic timing information for the instance.
  4. To improve theHttp2StreamandHttp2SessionThe closing mode of,Http2Stream.prototype.rstStream()The method is moved toHttp2Stream.prototype.close()
  5. inHttp2SessionNew attributes have been introduced to determine if a session is secure

About HTTP / 2:

Node. Js HTTP / 2 documenta…

Introduction to HTTP/2

util

  1. The new methodutil.isDeepStrictEqual(value1, value2)Can compare the depth of two values and return a Boolean value. We used to useassert.deepStrictEqual()The latter throws an exception if the two values are not equal.
  const { isDeepStrictEqual } = require('util');
   const isEqual = isDeepStrictEqual({	a: '1' }, { a: 1 });
   console.log(isEqual);	//false

   const { deepStrictEqual } = require('assert')
   const isEqual = deepStrictEqual({	a: '1' }, { a: 1 });
   // throw new errors.AssertionError
Copy the code
  1. The new methodutil.callbackify, you can convert promises into functions in the form of callback, suitable for scenarios that resolve compatibility issues:
  const { callbackify } = require('util')
   async function promiseDemo () {
     await Promise.resolve()
   }
   callbackify(promiseDemo)(function (err) {
     if (err) {
       return console.error(err)
     }
     console.log('finished without an error')
   })
Copy the code
  1. Allows for thedebuglog()The wildcard is used in theNODE_DEBUG=foo*Environment is running
 const util = require('util');
  const debuglog = util.debuglog('foo-bar');

  debuglog('hi there, it\'s foo-bar [%d]', 2333);
Copy the code

HTTP/1

  1. The HTTP module will now return a 400 status code when an incoming request cannot be successfully resolved. In the past, Node.js simply hung up the socket, causing other servers (such as Nginx) to mistakenly believe that the Node.js server was down.
  2. In previous versions, once a socket was assigned to a request,request.setTimeout()Is calledsocket.setTimeout(). This causes a timeout event to be issued on the request even if the underlying socket never connects. In Node.js 9,socket.setTimeout()Called only when the underlying socket successfully connected.
  3. This status code allows the server to send part of the header before the main header to preload the file.

More rigorous error codes

The Node.js core code base is slowly migrating to a new error system, with more rigorous error codes adopted in Node.js 9

Before Node.js 9, you might handle errors like this:

if (err.message === 'Console expects a writable stream instance') {
  //do something with the error
}
Copy the code

Now it should be handled this way:

if (err.code === 'ERR_CONSOLE_WRITABLE_STREAM') {
  //do something with the error
}
Copy the code

This can lead to various problems with the upgrade. To smooth the upgrade, Node.js officially gives the error code they use, which can be viewed here

Other changes

  1. The assert module’s methods can now throw any type of error (RangeError, SyntaxError, and so on). In previous versions of Node.js, these methods could only throw assertion errors.
  2. In previous versions of Node.js, if the timer’s delay overflowed, there was no indication that the overflow had occurred, whereas in Node.js 9, the timer issued a warning.
  3. NODE_OPTIONSA newstack-trace-limitProperty to set the stack upper limit in a development environment, using:NODE_OPTIONS=--stack-trace-limit=100
  4. supportconsole.debugMethod, this method andconsole.logPerformance is consistent
  5. cluster.settingsAllow through incwdProperty configures the destination directory
  6. When using programs like Electron, we need to control when to use v8 Platform, and in order for Node to work properly, we sometimes need to manually create NodePlatform. Node.js 9 adds a public API for creating/destroying NodePlatform
  7. Stream module, addedstate.endingProperty to determine whether the stream has been calledend()methods
  8. async_wrapTwo new attribute values are added:TCPSERVERWRAPPIPESERVERWRAP, allowing us to differentiate servers by connection type
  9. New asynchronous hooks have been added to provide an API to register callbacks to track all asynchronous resources in the application and record the timing of asynchronous operations they observe
  10. In n-API (the API for building native plug-ins), node.js 9 adds a function for plug-ins that need to reference the current event loop. This gives the plug-in access to the current event loop, but it is important to note that this feature is still experimental