1, single-threaded
Unlike other backing languages, node is single-threaded, significant savings in server spending
node does not create a new thread for each client, only one thread. Non-blocking I / O and
Event-driven mechanism, it is complicated by the macro point of view, can handle high concurrency.
2, non-blocking I / O
1) When we encounter asynchronous, can be solved by the following methods:
① callback function
When using asynchronous function, chasing touching a callback function as a parameter passed to the callback required when using
The require FS = const ( 'FS' ) // how to solve the asynchronous problem? // Method 1: solved by callback function getContent (CB) { fs.readFile ( 'a.txt', (ERR, Data) => { IF (ERR) { the console.log (ERR) return to false } CB (Data ) }) } getContent ((Data) => { the console.log (data.toString ()) })
② module using the events to listen
The require FS = const ( 'FS' ) const Events = The require ( 'Events' ) // how to solve the asynchronous problem? @ Method 2: by the node modules events // we mainly performed by broadcasting and receiving a broadcast objects EventEmitter const = EventEmitter new new events.EventEmitter () // receive broadcast to_parent performs only listen function getContent () { fs.readFile ( 'a.txt', (ERR, Data) => { IF (ERR) { the console.log (ERR) return to false } EventEmitter.emit ( 'Data' , Data) }) } getContent () EventEmitter.on ( ' data ', (data) => { console.log(data.toString()) })