Nodejs

“main” module

There is a widly followed convention in python to have a “main” function that is run when the script is called directly via cli: python script.py

def main():
  pass

if __name__ == "__main__":
  main()

A relatively unknown implementation exists for nodejs, like so:

function main() {}

if (require.main === module) main();

IMPORTANT: This only works for single run scripts and commonjs modules! ECMA Style “import” give module as undefined.

Websocket

Quick Server

require("http")
  .createServer((req, res) => {
    console.log(req.method + ": " + req.url);
    res.end("OK");
  })
  .listen(8080);

Testing

Readline

The node:readline module provides an interface for reading data from a Readable stream (such as process.stdin or fileReader) one line at a time.

About Timers

Worker Threads

Measuring Performance