ããã«ã¡ã¯ïŒ
ãã®Node.jsã¬ã€ãã®ç¿»èš³ã玹ä»ããŸãã
ãã®ä»ã®ããŒãïŒ
ããŒã1
ããŒã2
ããŒã3
ããŒã4
Node.jsã®æŠèŠ
Node.jsã¯ããªãŒãã³ã§ã¯ãã¹ãã©ãããã©ãŒã ã®JavaScriptã©ã³ã¿ã€ã ã§ããããã¯ãã»ãšãã©ãã¹ãŠã®ãããžã§ã¯ãã«æé©ãªãœãªã¥ãŒã·ã§ã³ã§ãã
Node.jsã¯ããã©ãŠã¶ã®å€éšã§ãGoogle Chromeã®ã³ã¢ã§ããJavaScriptãšã³ãžã³V8ãèµ·åããŸããããã«ãããNode.jsã®ããã©ãŒãã³ã¹ãåäžããŸãã
Node.jsã¢ããªã±ãŒã·ã§ã³ã¯ããªã¯ãšã¹ãããšã«æ°ããã¹ã¬ãããäœæããããšãªããåäžã®ããã»ã¹ãšããŠå®è¡ãããŸãã Node.jsã¯ãJavaScriptããããã¯ããä¿è·ããäžé£ã®éåæI / Oããªããã£ããæšæºã©ã€ãã©ãªã§æäŸããŸããéåžžãNode.jsã®ã©ã€ãã©ãªã¯ãéããããã³ã°ãã©ãã€ã ã䜿çšããŠèšè¿°ãããŠãããããã«ãŒã«ã§ã¯ãªãããããã³ã°åäœãäŸå€ã«ãªããŸãã
Node.jsãI / OæäœïŒãããã¯ãŒã¯ããã®ïŒããŒã¿ïŒã®èªã¿åããããŒã¿ããŒã¹ãŸãã¯ãã¡ã€ã«ã·ã¹ãã ãžã®ã¢ã¯ã»ã¹ãªã©ïŒãå®è¡ããŠããå Žåãã¹ã¬ããããããã¯ããŠCPUãµã€ã¯ã«ãå®äºããã®ãåŸ ã€ã®ã§ã¯ãªããNode.jsã¯å¿çãåãåã£ãåŸãæäœãå®è¡ãç¶ããŸãã
ããã«ãããNode.jsã¯ãé倧ãªãšã©ãŒã®åå ãšãªãã¹ã¬ããäžè²«æ§ã·ã¹ãã ãå¿ èŠãšããã«ãåäžã®ãµãŒããŒãä»ããŠæ°åã®ãªã¯ãšã¹ããåæã«åŠçã§ããŸãã
Node.jsã®å€§ããªå©ç¹ã¯ããã©ãŠã¶ãŒçšã®JavaScriptãäœæããäœçŸäžäººãã®éçºè ããå®å šã«ç°ãªãïŒããã°ã©ãã³ã°ïŒèšèªãåŠã¶å¿ èŠãªãã«ãã¯ã©ã€ã¢ã³ãåŽã³ãŒãã«å ããŠãµãŒããŒåŽã³ãŒããäœæã§ããããã«ãªã£ãããšã§ãã
Node.jsã§ã¯ãæ°ããECMAScriptæšæºãã·ãŒã ã¬ã¹ã«äœ¿çšã§ããŸãããã¹ãŠã®ãŠãŒã¶ãŒããã©ãŠã¶ãŒãæŽæ°ããã®ãåŸ ã€å¿ èŠã¯ãããŸãããNode.jsã®ããŒãžã§ã³ãå€æŽããŠã䜿çšããECMAScriptã®ããŒãžã§ã³ã決å®ããŸããNode.jsãå®è¡ããŠãè©Šéšçãªæ©èœãè¿œå ããããšãã§ããŸãã ïŒå¯Ÿå¿ããïŒãã©ã°ä»ãã
èšå€§ãªæ°ã®ã©ã€ãã©ãª
npmã¯ã·ã³ãã«ãªæ§é ã§Node.jsãšã³ã·ã¹ãã ã®æ¥éãªæé·ãæšé²ããŠãããçŸåš1,000,000ãè¶ ãããªãŒãã³ãœãŒã¹ããã±ãŒãžãç¡æã§äœ¿çšã§ããnpmã«ç»é²ãããŠããŸãã
Node.jsã¢ããªã±ãŒã·ã§ã³ã®ãµã³ãã«
Node.jsã®æãäžè¬çãªäœ¿çšäŸã¯ãWebãµãŒããŒãäœæããããšã§ãã
const http = require('http')
const hostname = '127.0.0.1'
const port = process.env.PORT
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}/`)
})
ãŸããhttpã¢ãžã¥ãŒã«ãå«ããŸãã
Node.jsã«ã¯ãäžæµã®ãããã¯ãŒã¯ãµããŒããå«ãçŽ æŽãããæšæºã©ã€ãã©ãªããããŸãã
ãã®ã¡ãœãã
createServer()
http
ã¯ãæ°ããHTTPãµãŒããŒãäœæããŠè¿ããŸãã
ãµãŒããŒã¯ç¹å®ã®ããŒããšãã¹ãã§åŸ æ©ããããã«èšå®ãããŠããŸãããµãŒããŒã®æºåãã§ãããšããµãŒããŒãå®è¡äžã§ããããšã瀺ãã³ãŒã«ããã¯é¢æ°ãããªã¬ãŒãããŸãã2ã€ã®ãªããžã§ã¯ããå«ã
æ°ããåŒã³åºãèŠæ±ã€ãã³ããªã¯ãšã¹ãïŒèŠæ±ïŒãªããžã§ã¯ãhttp.IncomingMessageïŒåä¿¡ã¡ãã»ãŒãžïŒïŒãšå¿çïŒãªããžã§ã¯ãhttp.ServerResponseïŒãµãŒããŒå¿çïŒïŒã
ãããã®ãªããžã§ã¯ãã¯ãHTTPåŒã³åºããåŠçããããã«å¿ èŠã§ãã
æåã®ãªããžã§ã¯ãã«ã¯ããªã¯ãšã¹ãã®è©³çŽ°ãå«ãŸããŠããŸãããã®äŸã§ã¯äœ¿çšããŸãããããªã¯ãšã¹ãããããŒãšãã®ããŒã¿ã«ã¢ã¯ã»ã¹ã§ããŸãã
2çªç®ã®ãªããžã§ã¯ãã¯ããªã¯ãšã¹ã¿ã«ããŒã¿ãæäŸããããã«äœ¿çšãããŸãã
ãã®å Žåã
res.statusCode = 200
ãªã¯ãšã¹ããæ£åžžã«å®äºããããšã瀺ãããã«ãstatusCodeããããã£ã200ã«èšå®ããŸãã
Content-TypeããããŒïŒã³ã³ãã³ããŸãã¯ã³ã³ãã³ãã¿ã€ãïŒãèšå®ããŸã
res.setHeader('Content-Type', 'text/plain')
ã«åŒæ°ãšããŠã³ã³ãã³ããè¿œå ããŠãå¿çãéããŸã
end()
ã
res.end('Hello World\n')
Node.jsãã¬ãŒã ã¯ãŒã¯ãšããŒã«
Node.jsã¯äœã¬ãã«ã®ãã©ãããã©ãŒã ã§ããéçºè ã®ããŒãºãæºããããã«ãäœåãã®ç°ãªãã©ã€ãã©ãªãäœæãããŠããŸãã
æéã®çµéãšãšãã«ããããã®å€ãã¯éåžžã«äººæ°ãåããŸããããããã®ããã€ãã®ãªã¹ãã次ã«ç€ºããŸãã
- AdonisJs: - , . Adonis â - Node.js.
- Express: -. , , .
- Fastify: -, . Fastify -.
- hapi: , .
- koa: Express . - , Express.
- Loopback.io: .
- Meteor: -, JavaScript, . - meteor , , React, Vue Angular. .
- Micro: HTTP-.
- NestJS: TypeScript Node.js- , .
- Next.js: React- .
- Nx: NestJS, Express, React, Angular .. Nx , .
- Socket.io: .
Node.js
ä¿¡ããããªããããããŸããããNode.jsã¯ãŸã 10幎ããçµã£ãŠããŸããã
æ¯èŒãããšãJavaScriptã¯24幎åãããããWebã¯30幎åãããããŸãã
10幎ãšããã®ã¯ãã¯ãããžãŒã«ãšã£ãŠã¯çãæéã§ãããNode.jsãåžžã«ããã§ããããã«èŠããããšããããŸãã
Node.jsãç¥ãå§ããŠãã2幎ããçµã£ãŠããŸããããããã§ãæ å ±ãéãããŠããã«ãããããããçŽ æŽãããæªæ¥ãåŸ ã¡åããŠããããã«æããŸããã
ãã®ã»ã¯ã·ã§ã³ã§ã¯ãNode.jsã®å±¥æŽã®å šäœåãèŠãŠãããŸãã
ã¡ãã£ãšããæŽå²
JavaScriptã¯ãNetscape Navigatorãã©ãŠã¶ã§WebããŒãžãæäœããããã®ã¹ã¯ãªããããŒã«ãšããŠNetscapeã«ãã£ãŠçºæãããããã°ã©ãã³ã°èšèªã§ãã
Netscapeããžãã¹ã¢ãã«ã®äžéšã¯ããµãŒããŒãµã€ãJavaScriptã䜿çšããŠåçããŒãžãäœæã§ããNetscape LiveWireãå«ãWebãµãŒããŒã®è²©å£²ã§ãããæ®å¿µãªãããNetscape LiveWireã¯å€±æãããµãŒããŒãµã€ãJavaScriptã¯Node.jsãŸã§æ®åããŸããã§ããã
Node.jsã®äººæ°ã®äž»èŠãªèŠçŽ ã®1ã€ã¯ããã®ã¿ã€ãã³ã°ã§ããæ°å¹ŽåãJavaScriptã¯ãFlickrãGmailãªã©ã®Web 2.0ã¢ããªã±ãŒã·ã§ã³ã®ãããã§ãææ°ã®Webãã©ã®ããã«èŠããããäžçã«ç€ºãããããã§ãæ·±å»ãªïŒããã°ã©ãã³ã°ïŒèšèªãšããŠèªèãããŠããŸããã
ãã©ãŠã¶ããŠãŒã¶ãŒã®å©çã®ããã«ããã©ãŒãã³ã¹ãåäžãããããšãããããJavaScriptãšã³ãžã³ãå€§å¹ ã«æ¹åãããŸãããã¡ã€ã³ãã©ãŠã¶ã®éçºè ããŒã ã¯ãJavaScriptã®æé©ãªãµããŒããšãã®æéã®å®è¡ãå®è£ ããããã«æžåœã«åãçµã¿ãŸãããNode.jsã䜿çšãããšã³ãžã³ãV8ïŒChrome V8-Chromiumãããžã§ã¯ãã®ãªãŒãã³ãœãŒã¹JavaScriptãšã³ãžã³ãšããŠãç¥ãããŠããŸãïŒã¯ããã®ç«¶äºããåå©ãåããŸããã
Node.jsã¯é©åãªã¿ã€ãã³ã°ã§é©åãªå Žæã«é 眮ãããŸããã幞ãã«ããããã人æ°ã®å¯äžã®çç±ã§ã¯ãããŸããããã®ãããžã§ã¯ãã«ã¯ãå€ãã®éçºè ã楜ããã ãµãŒããŒãµã€ãJavaScriptéçºã«å¯Ÿããé©æ°çãªã¢ã€ãã¢ãšã¢ãããŒããæ°å€ãå«ãŸããŠããŸããã
2009幎
- Node.jsç»å Ž
- Npmç»å Ž
2010
- æ¥è¡
- Socket.io
2011幎
- npmããŒãžã§ã³1.0
- Node.js: LinkedIn, Uber .
- hapi
2012
- Node.js
2013
- - Node.js: Ghost
- Koa
2014
- : Node.js io.js ( â git) ES6
2015
- Node.js Foundation
- IO.js Node.js ( â git)
- npm ()
- Node.js 4 ( 1, 2 3 )
2016
2017
- npm
- Node.js 8
- HTTP/2
- V8 Node.js , Node.js JS Chrome
- 3 npm
2018
- Node.js 10
- .mjsæ¡åŒµåã«ããå®éšçãªESã¢ãžã¥ãŒã«ã®ãµããŒã
Node.jsãã€ã³ã¹ããŒã«ããã«ã¯ã©ãããã°ããã§ããïŒ
Node.jsã¯ããŸããŸãªæ¹æ³ã§ã€ã³ã¹ããŒã«ã§ããŸãã
äž»èŠãªãã©ãããã©ãŒã ã®ãã£ã¹ããªãã¥ãŒã·ã§ã³ã¯ãå ¬åŒWebãµã€ãã§å ¥æã§ããŸãã
Node.jsãã€ã³ã¹ããŒã«ããéåžžã«äŸ¿å©ãªæ¹æ³ã¯ãããã±ãŒãžãããŒãžã£ãŒã䜿çšããããšã§ããåãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«ã¯ç¬èªã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ããããŸãã
macOSã§ã¯ãããã¯Homebrewã§ããããã«ãããã³ãã³ãã©ã€ã³ã䜿çšããŠNode.jsãç°¡åã«ã€ã³ã¹ããŒã«ã§ããŸãã
brew install node
LinuxãWindowsãããã³ãã®ä»ã®ã·ã¹ãã çšã®ããã±ãŒãžãããŒãžã£ãŒã®ãªã¹ãã¯ãã¡ãã§ãã
nvm
Node.jsãå®è¡ããäžè¬çãªæ¹æ³ã§ããNode.jsã®ããŒãžã§ã³éã®åãæ¿ãããã¹ãçšã®æ°ããããŒãžã§ã³ã®ã€ã³ã¹ããŒã«ãåé¡ãçºçããå Žåã®ã€ã³ã¹ããŒã«ã®åãæ¶ããç°¡åã«è¡ãããšãã§ããŸãã
以åã®ããŒãžã§ã³ã®Node.jsã§ã³ãŒãããã¹ãããå¿ èŠãããå Žåã«ãéåžžã«åœ¹ç«ã¡ãŸãã
詳现ã«ã€ããŠã¯
nvm
ããã®ãªã³ã¯ãã¯ãªãã¯ããŠãã ããã
å§ããã°ããã§ãHomebrewã以åã«äœ¿çšããããšããªãå Žåã¯ãå ¬åŒã€ã³ã¹ããŒã©ãŒã䜿çšããããšããå§ãããŸãã
Node.jsãã€ã³ã¹ããŒã«ãã
node
ãšãã³ãã³ãã©ã€ã³ããå®è¡å¯èœããã°ã©ã ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã
Node.jsã䜿çšããããã«JavaScriptãã©ã®çšåºŠç解ããå¿ èŠããããŸããïŒ
åå¿è ã«ãšã£ãŠãããã°ã©ãã³ã°ã¬ãã«ã決å®ããããšã¯å°é£ã§ãã
ãŸããJavaScriptãçµäºããŠNode.jsãéå§ããå Žæãããã³ãã®éãç¹å®ããããšãå°é£ã§ãã
å人çã«ã¯ãNode.jsã«é²ãåã«ã以äžã®åºæ¬çãªJavaScriptã®æŠå¿µãååã«ç解ããŠããããšããå§ãããŸãã
- æ§æãŸãã¯åå¥æ§é
- åŒïŒåºæ¬çã«åãæ§æïŒ
- ããŒã¿åïŒ
- å€æ°
- é¢æ°
- ãã®ããŒã¯ãŒã
- ç¢å°é¢æ°
- ã«ãŒã
- å¯èŠé å
- é å
- ãã³ãã¬ãŒããŸãã¯æååãªãã©ã«
- ã»ãã³ãã³ïŒããããããã䜿çšããããšãå¿ é ã®å Žåãããšãã°IIFEã§äœæ¥ããå ŽåïŒ
- å³ããäœå¶
- ECMAScript 6ã2016ã2017
ãããã®æŠå¿µãç¿åŸããããšã¯ãããã®ãã«ã¹ã¿ãã¯éçºè ã®éã®å§ãŸãã§ãã
次ã®æŠå¿µã¯ãNode.jsã®åºæ¬çãªéšåã§ããéåæããã°ã©ãã³ã°ãç解ããäžã§ãéèŠã§ãã
- éåæããã°ã©ãã³ã°ãšã³ãŒã«ããã¯ïŒã³ãŒã«ããã¯ïŒ
- ã¿ã€ããŒïŒã«ãŠã³ã¿ãŒïŒ
- çŽæïŒçŽæïŒ
- éåæ/åŸ æ©
- ç絡
- ã€ãã³ãã«ãŒãïŒã³ãŒã«ã¹ã¿ãã¯ïŒ
Node.jsãšãã©ãŠã¶ã®éã
JavaScriptã¯ããã©ãŠã¶ãŒãšNode.jsã®äž¡æ¹ã§äœ¿çšã§ããŸãã
ãã ãããã©ãŠã¶ã¢ããªã®æ§ç¯ã¯ãNode.jsã¢ããªã®æ§ç¯ãšã¯å€§ããç°ãªããŸãã
JavaScriptãã©ã¡ãã®å Žåã§ã䜿çšããããšããäºå®ã«ãããããããéçºã®è©³çŽ°ã決å®ããããã€ãã®éèŠãªéãããããŸãã
JavaScriptããã³ããšã³ãïŒããã³ããšã³ãéçºè -ã¢ããªã±ãŒã·ã§ã³ã®ã¯ã©ã€ã¢ã³ãåŽïŒã®èŠ³ç¹ããèŠããšãNode.jsã§ã¢ããªã±ãŒã·ã§ã³ãéçºããããšã«ã¯å€§ããªå©ç¹ããããã¯ã©ã€ã¢ã³ããšãµãŒããŒã®äž¡æ¹ã§åãããã°ã©ãã³ã°èšèªã䜿çšããããšããäºå®-JavaScriptã
ããã¯ããã«ã¹ã¿ãã¯ã«ãªã絶奜ã®æ©äŒã§ããç§ãã¡ã¯çãæ°ããããã°ã©ãã³ã°èšèªãå®å šã«æ·±ãåŠã¶ããšãã©ãã»ã©é£ããããç¥ã£ãŠããŸãããã®å Žåããã®ãããªå¿ èŠã¯ãããŸããã
ç 究ããå¿ èŠãããã®ã¯çæ ç³»ã ãã§ãã
ãã©ãŠã¶ãŒã§ã¯ãã»ãšãã©ã®å ŽåãDOMãCookieãªã©ã®ä»ã®Web APIãåŠçããå¿ èŠããããŸãããã¡ããããããã¯Node.jsã«ã¯ååšããŸããã Node.jsã«ã¯ããŠã£ã³ããŠãããã¥ã¡ã³ãããã®ä»ã®ãã©ãŠã¶åºæã®ãªããžã§ã¯ãããããŸããã
äžæ¹ããã©ãŠã¶ã«ã¯ãNode.jsããã¡ã€ã«ã·ã¹ãã ã¢ã¯ã»ã¹ãªã©ã®ã¢ãžã¥ãŒã«ãéããŠæäŸããåªããAPIããããŸããã
ãã1ã€ã®å€§ããªéãã¯ãNode.jsã§ã¯ã³ãŒãã©ã³ã¿ã€ã ãå¶åŸ¡ããããšã§ãã誰ã§ãã©ãã§ããããã€ã§ãããªãŒãã³ãœãŒã¹ã¢ããªã±ãŒã·ã§ã³ãéçºããŠããã®ã§ãªãéããã¢ããªã±ãŒã·ã§ã³ã®å®è¡ã«ã©ã®ããŒãžã§ã³ã®Node.jsã䜿çšãããŠããããããããŸããããã¯ããŠãŒã¶ãŒã䜿çšãããã©ãŠã¶ãŒã決å®ã§ããããããã©ãŠã¶ãŒã©ã³ã¿ã€ã ãšã¯å¯Ÿç §çã«éåžžã«äŸ¿å©ã§ãã
ããã¯ãNode.jsã®ããŒãžã§ã³ã§ãµããŒããããŠããJavaScriptã³ãŒããèšè¿°ã§ããããšãæå³ããŸãã
JavaScriptã¯éåžžã«åçã«éçºãããŠããããããã©ãŠã¶ã«ã¯åžžã«èšèªã®æ°æ©èœãè¿ éã«å®è£ ããæéããªãããŠãŒã¶ãŒã¯åžžã«ã¿ã€ã ãªãŒã«æŽæ°ããããã§ã¯ãªããããWebã§ã¯ãã°ãã°å€ãJavaScriptãç¶æããå¿ èŠããããŸãã
ã³ãŒããES5ã«ãã©ã³ã¹ãã€ã«ããã«ã¯ãBabelã䜿çšã§ããŸããNode.jsã§ã¯ãããã¯å¿ èŠãããŸããã
ãã1ã€ã®éãã¯ãNode.jsãCommonJSã¢ãžã¥ã©ãŒã·ã¹ãã ã䜿çšãããã©ãŠã¶ãŒãESã¢ãžã¥ãŒã«ããµããŒãããããšã§ãã
å®éã«ã¯ãããã¯Node.jsã§ã¯ã䜿çš
require()
ãããã©ãŠã¶ã§ã¯ã䜿çšããããšãæå³ãimport
ãŸãã
ãéèŽããããšãããããŸããããšã©ãŒãã¿ã€ããã¹ãèŠã€ããå Žåã¯ãå人ã§æžããŠãã ãããç§ã¯æè¬ããŸãã
ã€ã¥ãâŠ