In-depth understanding of Node.js
  • README
  • Introduction to Node.js
    • Overview of the architecture
    • Why libuv?
    • V8 concepts
    • C++ and JS interaction
  • Starting with hello world
  • Module loading
  • Global object
  • Event loop
  • Timer interpretation
  • Yield magic
  • Buffer
  • Event
  • Domain
  • Stream
  • Net
    • Socket
    • Building applications
    • Encryption
  • HTTP
    • HTTP Server
    • HTTP Client
  • FS file system
    • File system
    • File abstraction
    • IO
    • libuv selection
    • File IO
    • Fs essence
  • Process
    • Process
    • Cluster
  • Node.js pitfalls
  • Others
    • Node.js & Android
    • Node.js & Docker
    • Node.js tuning
  • Appendix
Powered by GitBook
On this page

Starting with hello world

Starting with "Hello World"

Let's start with a piece of code that is familiar to everyone, https://nodejs.org/en/about/. Like learning any language, a simple "Hello World" program can give you an intuitive understanding of node.js.

const http = require('http');
const hostname = '127.0.0.1';
const port = 1337;

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Let's take a look at how many core modules are involved in the first line of code, and start our journey of analyzing node.js source code.

The first line: const http = require('http'); involves two modules, the module and http modules.

Main code

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port, hostname, () => {
  ...
});
  • First, let's understand the inheritance relationship of the HTTP Server, which is helpful for a better understanding of the code.

This also involves the event and net modules.

The last sentence:

console.log(`Server running at http://${hostname}:${port}/`);

Here, the console module is used, but it is not obtained through require. This brings us to the global object, the top-level object of Node.js. I'll leave it as a teaser for now, and I'll explain it in detail in the global section later.

If you want to view some debugging logs of node, you can set the NODE_DEBUG environment variable, for example:

NODE_DEBUG=HTTP,STREAM,MODULE,NET node http.js

View V8 logs

node --trace http.js

Summary

A simple hello world program involves multiple modules, but behind it is the wisdom of the Node community, a high-level abstraction of web services and asynchronous IO. As the saying goes, simplicity is the ultimate sophistication!

Let's start the journey of Node.js source code with a famous quote from Linus Torvalds.

Talk is cheap, show me the code.

PreviousC++ and JS interactionNextModule loading

Last updated 1 year ago

http