As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications. In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done, Node will sleep. const http = require ( 'http' ) ; const hostname = '127.0.0.1' ; const port = 3000 ; const server = http . createServer ( ( req , res ) = > { res . statusCode = 200 ; res . setHeader ( 'Content-Type' , 'text/plain' ) ; res . end ( 'Hello World\n' ) ; } ) ; server . listen ( port , hostname , ( ) = > { console . log ( `Server running at http:// ${ hostname } : ${ port } /` ) ; } ) ; This is in contrast to today's more common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node are free from worries o...
Comments
Post a Comment