What steps does Node.js takes to execute a program?

前端 未结 2 1726
臣服心动
臣服心动 2021-01-16 15:33

I\'m exploring node.js for quite some time now. But yet haven\'t figured out exactly, how a program executes.

For e.g. taking this simple program:

/         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 16:15

    1. Node is single-threaded. While the blocking call is executing, nothing happens. No events will fire, no callbacks will execute. This is why blocking calls are always highly discouraged in the Node world. ReadFileSync should be used for command line scripts or perhaps when bootstrapping your app.
    2. Not quite sure what it is that you're asking exactly. In your listed example, the callback receives (as is very common) two arguments. One is either null or an Error instance and the other is the data. This method receives a Buffer as the second argument. A quick-and-easy way to convert a buffer to a string is data.toString() which converts the list of bytes into a UTF-8 encoded string.

    You'll notice that pretty much every Node module uses this callback signature; an optional Error and then the data.

    When running from the command line, you'll notice that the app quits when the last IO action is finished. Node keeps track of this itself. When building a web app, the open HTTP(S) connection will keep the process 'busy'.

提交回复
热议问题