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:
- Added support for Alternative HTTP Services (ALTSVC)
- new
maxSessionMemory
, which limits the maximum amount of memory allowed for a single HTTP2 thread. If this value is exceeded, http2 requests will be rejected - Collect and report relevant information
Http2Session
和Http2Stream
Basic timing information for the instance. - To improve the
Http2Stream
andHttp2Session
The closing mode of,Http2Stream.prototype.rstStream()
The method is moved toHttp2Stream.prototype.close()
中 - in
Http2Session
New attributes have been introduced to determine if a session is secure
About HTTP / 2:
Node. Js HTTP / 2 documenta…
Introduction to HTTP/2
util
- The new method
util.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
- The new method
util.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
- Allows for the
debuglog()
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
- 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.
- 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. - 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
- 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.
- 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.
NODE_OPTIONS
A newstack-trace-limit
Property to set the stack upper limit in a development environment, using:NODE_OPTIONS=--stack-trace-limit=100
- support
console.debug
Method, this method andconsole.log
Performance is consistent cluster.settings
Allow through incwd
Property configures the destination directory- 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
- Stream module, added
state.ending
Property to determine whether the stream has been calledend()
methods async_wrap
Two new attribute values are added:TCPSERVERWRAP
和PIPESERVERWRAP
, allowing us to differentiate servers by connection type- 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
- 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