JavaScript
Require Modules from the Command Line
io.js v1.6.0 introduces a new command line option: -r
or --require
, which can be used to pre-load modules at startup, see:
https://github.com/iojs/io.js/blob/v1.x/CHANGELOG.md#2015-03-19-version-160-chrisdickinson
I can think of one interesting use case: overriding the default behavior or console.log
to perform deep inspection:
|
|
Now I can pre-load this script to have all console log show in better format:
|
|
Time to find out more use cases.
Test Execution Order in Mocha
In synchronous tests, both describe
and it
statements are executed in the order they are laid out.
Test Foo
should run before Bar
:
|
|
Suite Foo
should run before Bar
:
|
|
In asynchronous tests, the same ordering applies.
Test Foo should run before Bar even Foo takes much longer to execute, and Suite B should also run after Suite A:
|
|
Result:
|
|
This is the great feature in Mocha.
SuperTest: Listen at Random Port
In the example section of SuperTest (v0.15.0), it mentions something about ephemeral port:
You may pass an
http.Server
, or aFunction
torequest()
- if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports. - https://github.com/visionmedia/supertest#example
Let’s dissect this sentence. There are a few key concepts to grasp:
- The
request
function will accept either a function or anhttp.Server
object. - When does the server not listening for connections?
- What is an ephemeral port?
Taking a peek at the index.js
file in SuperTest source code, it is easy to see that it accepts both function and http.Server
object, but prefer latter:
|
|
Why it does this? This has to do with Express:
|
|
Only when initiating an Express app, it returns a function, not an object. And follows up on lib/test.js
, when SuperTest detects the created server is yet to bind to any port number, it will invoke app.listen(0)
, so called ephemeral port. In fact, it is just a random port.
When something is ephemeral, it last for a very short time. When allowing a server to accept connections, we usually do is setting the server to listen on a specific port:
|
|
What if setting this to 0
like above or omit this port number?
Locate HTTP Port Number from http.Server Instance
This is a bare minimal HTTP server:
|
|
If the port
number is evaluated to be falsy, such as 0
, null
, undefined
or an empty string, a random port will be assigned.
Begin accepting connections on the specified
port
andhostname
. If thehostname
is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. A port value of zero will assign a random port. - https://iojs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
We can use command line tool such as netstat
to find out the port number:
|
|
However, it will be very convenient without relying on external tools.
Luckily, we can find out the port number by using the address
method:
|
|
The result will be similar to:
|
|
Furthermore, it works with all popular libraries that create an http.Server
instance:
Use Array of Middleware in Koa Router
Koa Router v4.2 does not support an array of middleware, multiple middleware must be entered one by one:
|
|
Using an array is much cleaner than multiple arguments.
|
|
But the above will throw error:
|
|
This can be easily fixed by using Koa Compose:
|
|
Hence:
|
|
Use PM2 with io.js
If you have both Node.js and io.js installed, PM2 will run Node.js by default. To start the app with io.js, use --interpreter
flag:
|
|