About | Node.js
文章推薦指數: 80 %
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. In the following "hello world" example, ... About Governance AboutNode.js®Asanasynchronousevent-drivenJavaScriptruntime,Node.jsisdesignedtobuild scalablenetworkapplications.Inthefollowing"helloworld"example,many connectionscanbehandledconcurrently.Uponeachconnection,thecallbackis fired,butifthereisnoworktobedone,Node.jswillsleep. consthttp=require('http'); consthostname='127.0.0.1'; constport=3000; constserver=http.createServer((req,res)=>{ res.statusCode=200; res.setHeader('Content-Type','text/plain'); res.end('HelloWorld'); }); server.listen(port,hostname,()=>{ console.log(`Serverrunningathttp://${hostname}:${port}/`); }); Thisisincontrasttotoday'smorecommonconcurrencymodel,inwhichOSthreads areemployed.Thread-basednetworkingisrelativelyinefficientandvery difficulttouse.Furthermore,usersofNode.jsarefreefromworriesof dead-lockingtheprocess,sincetherearenolocks.Almostnofunctionin Node.jsdirectlyperformsI/O,sotheprocessneverblocksexceptwhentheI/Oisperformedusing synchronousmethodsofNode.jsstandardlibrary.Becausenothingblocks,scalablesystemsarevery reasonabletodevelopinNode.js. Ifsomeofthislanguageisunfamiliar,thereisafullarticleon Blockingvs.Non-Blocking. Node.jsissimilarindesignto,andinfluencedby,systemslikeRuby's EventMachineandPython'sTwisted.Node.jstakestheeventmodelabit further.Itpresentsaneventloopasaruntimeconstructinsteadofasalibrary.Inothersystems, thereisalwaysablockingcalltostarttheevent-loop. Typically,behaviorisdefinedthroughcallbacksatthebeginningofascript,and attheendaserverisstartedthroughablockingcalllikeEventMachine::run(). InNode.js,thereisnosuchstart-the-event-loopcall.Node.jssimplyenterstheeventloopafterexecutingtheinputscript.Node.js exitstheeventloopwhentherearenomorecallbackstoperform.Thisbehavior islikebrowserJavaScript—theeventloopishiddenfromtheuser. HTTPisafirst-classcitizeninNode.js,designedwithstreamingandlow latencyinmind.ThismakesNode.jswellsuitedforthefoundationofaweb libraryorframework. Node.jsbeingdesignedwithoutthreadsdoesn'tmeanyoucan'ttake advantageofmultiplecoresinyourenvironment.Childprocessescanbespawned byusingourchild_process.fork()API,andaredesignedtobeeasyto communicatewith.Builtuponthatsameinterfaceistheclustermodule, whichallowsyoutosharesocketsbetweenprocessestoenableloadbalancing overyourcores. ↑Scrolltotop
延伸文章資訊
- 1Node.js - 維基百科,自由的百科全書
Node.js 大部分基本模組都用JavaScript 語言編寫。在Node.js 出現之前,JavaScript 通常作為使用者端程式設計語言使用,以JavaScript 寫出的程式常在使用者...
- 2Introduction to Node.js
Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool...
- 3What Is Node.js and Why You Should Use It
Node.js is a single-threaded, open-source, cross-platform runtime environment for building fast a...
- 4Node.js - Wikipedia
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a...
- 5Node.js - Introduction - Tutorialspoint
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable ...