éå§ãã
æ°é±éåã«ãååãšããã€ãã®ä»®å®ããã¹ãããããã«èšèšãããéåžžã«åçŽãªãããã¿ã€ãWebã¢ããªã±ãŒã·ã§ã³ãäœæããå¿ èŠããã£ããšãã«ãåè¿°ã®äžé£ã®ãã¯ãããžãŒã®äœ¿çšãéå§ããŸããã
ç§ã®å®éšã¯å€§æåã§ããã ç§ã¯éåžžã«è¿ éã«ãããã¿ã€ããäœæããããšãã§ããååã¯ããã䟿å©ã«å®éšããããšãã§ãã圌ãã¯ããã«ã€ããŠã®å°è±¡ãè¿ éã«è¡šçŸããããšãã§ããŸããã åæã«ãNode.jsãšNPMãã³ã³ãã¥ãŒã¿ãŒã«ã€ã³ã¹ããŒã«ãããŠããªããŠãããããžã§ã¯ãããã¹ãã§ããŸããã
ããããã¹ãŠããWebã¢ããªã±ãŒã·ã§ã³ã®ã©ããããããã¿ã€ãã³ã°ãžã®ã¢ãããŒãã«é¢ããè³æãæžãã¹ãã ãšããèãã«è³ããŸããã ãã®ã¢ãããŒãã¯ãä»ã®èª°ãã«åœ¹ç«ã€å¯èœæ§ããããŸãã FastifyãšPreactã«æ¢ã«ç²ŸéããŠãã人ã®ããã«ãæãéèŠãªããšãããã«èª¬æããŸããããã«ãããç§ã®ã¢ã€ãã¢ãããã«å®è·µã§ããŸãã
äž»ãªã¢ã€ãã¢
ãã§ã«FastifyãšPreactã«ç²ŸéããŠããããããã®ãã¯ãããžãŒã«åºã¥ããŠãããžã§ã¯ãã®éçºãæŽçããæ¹æ³ãåŠã³ããå Žåã¯ãæåéããããªããæããã®ããããã€ãã®ã¹ãããã§ãã ã€ãŸãã次ã®ã³ãã³ãã«ã€ããŠè©±ããŠããïŒ
git clone https://github.com/lmammino/fastify-preact-htm-boilerplate.git my-new-project cd my-new-project rm -rf .git npm install
ãã¡ããããããžã§ã¯ãã®åå
my-new-project
ãããžã§ã¯ãã®ååã«å€æŽã§ããŸãã
å¿ èŠãªãã®ããã¹ãŠã€ã³ã¹ããŒã«ãããããããžã§ã¯ãã®äœæ¥ãéå§ã§ããŸãã ã€ãŸããç§ãã¡ã¯æ¬¡ã®ããšã«ã€ããŠè©±ããŠããïŒ
- ã¢ããªã±ãŒã·ã§ã³ã®ã¯ã©ã€ã¢ã³ãéšåã®ãã¡ã€ã«ã¯ã
src/ui
ãã©ã«ããŒã«åéãããŸãïŒããã§ã¯Preactãšhtmã䜿çšãããŸãïŒã -
src/server
ãã©ã«ããŒã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒéšåã«é¢é£ãããã¡ã€ã«ãå«ãŸããŸãïŒããã§ã¯ãFastifyã䜿çšãããŸãïŒã
é©åãªãã¡ã€ã«ãç·šéããããšã«ããããããžã§ã¯ããå®è¡ã§ããŸãã
npm start
ãã®åŸããã©ãŠã¶ã§
localhost:3000
ã¢ãã¬ã¹ã«ç§»åããŠãã¹ãã§ããŸãã
ãããŠããäžã€ã ç§ã®éçºãæ°ã«å ¥ã£ããã GitHubã®ã¹ã¿ãŒã«éåžžã«æè¬ããŸã ã
ããã§ãããã§äœ¿çšãããŠãããã¯ãããžãŒãšãããããšé£æºããæ©èœãèŠãŠã¿ãŸãããã
åºå®ãã
Fastifyã¯Node.jsçšã®é«éã§çµæžçãªWebãã¬ãŒã ã¯ãŒã¯ã§ãã ãã®ãããžã§ã¯ãã¯ããšããš2人ã®ããã°ã©ããŒã«ãã£ãŠäœæãããŸããã çŸåšããã®ãããžã§ã¯ãã«åãçµãã§ããããŒã ã¯10人ã§ã130人以äžããããžã§ã¯ãã®éçºã«ååããŠããŸãã圌ã¯GitHubã§ã»ãŒ10,000ã®æãéããŸããã
Fastifyã¯ãExpressãHapiãªã©ã®Node.jsãã¬ãŒã ã¯ãŒã¯ã®åœ±é¿ãåããŠããŸããã ããšããšã¯ãçç£æ§ãããã°ã©ããŒã®å©äŸ¿æ§ããã©ã°ã€ã³ã®å©ããåããŠæ©èœãæ¡åŒµããããšãç®çãšããŠããŸããã ã¡ãªã¿ã«ãããã¯ç§ã®ãæ°ã«å ¥ãã®Fastifyæ©èœã®1ã€ã§ãã
Fastifyãã¬ãŒã ã¯ãŒã¯ã«æ £ããŠããªãå ŽåããFastifyãã¬ãŒã ã¯ãŒã¯ã«ã€ããŠè©³ããç¥ãããå Žåã¯ãå ¬åŒããã¥ã¡ã³ãããå§ãããŸã ã
Fastifyã«é¢é£ããŠããããšã«æ³šæããŠãã ããã ç§ã¯ã¡ã€ã³éçºããŒã ã®ã¡ã³ããŒã§ãããäž»ã«ãããžã§ã¯ããµã€ãã®ãµããŒããšããã¥ã¡ã³ãã®äœæã«åãçµãã§ããŸãã
ããªã¢ã¯ã
Preactã¯ãWebãããžã§ã¯ãã®ãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãéçºããããã®ã©ã€ãã©ãªã§ãReactã®ã³ã³ãã¯ãã§è¿ éãªä»£æ¿ãšããŠ1人ãäœæããŸããã ãã®ãããžã§ã¯ãã¯éåžžã«æåããããšãå€æããŸãããçŸåšãéçºè ã®ããŒã å šå¡ãGitHubã§20,000以äžã®æãç²åŸããŠããŸãã
ç§ãPreactãæ°ã«å ¥ã£ãŠããçç±ã®1ã€ã¯ããã®ã©ã€ãã©ãªã«ã¯ã¢ããªã±ãŒã·ã§ã³ã®èŠèŠã³ã³ããŒãã³ããèšè¿°ããããã®æ¡åŒµå¯èœãªã¬ã€ã€ãŒãããããšã§ãã éåžžã®ç¶æ³ã§ã¯ããã®ã©ã€ãã©ãªã¯JSXãšBabelãçµã¿åãããŠäœ¿çšââããŠã³ãŒããå€æã§ããŸãããBabelãã€ã³ã¹ããŒã«ããŠã¢ããªã±ãŒã·ã§ã³ã®ãã«ãããã»ã¹ãæ§æããããªãå Žåã¯ãããšãã°ãã³ãã¬ãŒããªãã©ã«ã䜿çšããå¿ èŠãšããªãhtmã©ã€ãã©ãªãšãšãã«Preactã䜿çšã§ããŸãææ°ã®ãã©ãŠã¶ã§äœ¿çšãããŠãããããžã§ã¯ããèµ·åãããšãã®ãã©ã³ã¹ãã¬ãŒã·ã§ã³ã
ãã®è³æã§ã¯htmã©ã€ãã©ãªã䜿çšããããã«ããã€ãã®äŸãæ€èšããŸãã
ãããžã§ã¯ãã®æŠèŠ
ããã§ã¯ããããžã§ã¯ããäœæããããã»ã¹å šäœãèŠãŠãããŸãã ç§ãã¡ã®ç®æšã¯ãèµ·åæã®ãµãŒããŒã®æéã«é¢ããæ å ±ã衚瀺ããåçŽãªWebã¢ããªã±ãŒã·ã§ã³ãéçºããããšã§ãã ããã§ãç§ãã¡ãäœãç®æããŠããã®ããããæ確ã«ããããã«ã
ãã©ãŠã¶ã§ã®ã¢ããªã±ãŒã·ã§ã³
ããã¯ã·ã³ã°ã«ããŒãžã¢ããªã±ãŒã·ã§ã³ïŒSPAïŒã§ãPreactãšhtmã䜿çšããŠã¯ã©ã€ã¢ã³ãéšåã圢æããFastifyã䜿çšããŠãµãŒããŒæéãåä¿¡ããããã«èšèšãããAPIãäœæããŸãã
泚ææ·±ãèªè ã¯ãåã®å³ã«ç€ºãããŠããããŒãžã«çŽ æµãªãã¡ãã³ã³ã¢ã€ã³ã³ãããããšã«æ°ä»ããããããŸããã 確ãã«ãããã¯éåžžã«å°ããã®ã§ãç®ãã€ã¶ã£ãŠè§£æ±ºããããšãã人ã®ããã«ã¿ã¹ã¯ãä¿é²ããŸãã 以äžã«æ¡å€§çã瀺ããŸãã
ãã¡ãã³ã³
ã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒåŽã®æ§æ
æ°ãããã©ã«ããŒãäœæããããšããå§ããŸãããïŒ
mkdir server-time cd server-time
NPMãããžã§ã¯ããåæåããFastifyãã€ã³ã¹ããŒã«ããŸãã
npm init -y npm i --save fastify@next fastify-static@next fastify-cli
ããã€ãã®äŸå
@next
ã説æãããšãã«
@next
ã³ã³ã¹ãã©ã¯ãã䜿çšããããšã«æ³šæããŠãã ããã ããã¯ããããžã§ã¯ããFastify 2ã©ã€ãã©ãªã䜿çšããããšãä¿èšŒããããã«è¡ãããŸãããã®ã©ã€ãã©ãªã¯çŸåšãªãªãŒã¹åè£ã®ç¶æ ã§ããããŸããªãã¡ã€ã³ã®å®å®ããŒãžã§ã³ã«ãªããŸãã
fastify-cli
ã³ãã³ãã©ã€ã³
fastify-cli
ã䜿çšããŠã
fastify-cli
åºã¥ããŠæ°ãããããžã§ã¯ããäœæããããšãã§ã
fastify-cli
ã
npx fastify-cli generate server-time
ãã®è³æã®å·çæç¹ã§ããã®ããŒã ã¯Fastify 1.xã䜿çšããããã«èšèšããããããžã§ã¯ããäœæããŸãããFastify 2ã®ãªãªãŒã¹åŸããã«ããã®ããŒã«ãæŽæ°ãããŸãã
ã€ã³ã¹ããŒã«ãããããã±ãŒãžãåæããŸãããïŒ
-
fastify
ã¯ããã¬ãŒã ã¯ãŒã¯ã®ã³ã¢ã³ã³ããŒãã³ãã§ãã -
fastify-static
ã¯ãFastifyãµãŒããŒã§éçãã¡ã€ã«ã䟿å©ã«æäŸã§ããè¿œå ã®ãã©ã°ã€ã³ã§ãã -
fastify-cli
ã¯ãFastifyã«åºã¥ããŠãããžã§ã¯ããäœæã§ããã³ãã³ãã©ã€ã³ããŒã«ã§ãã
çŸåšãFastifyã«åºã¥ããŠAPIãäœæããæºåãã§ããŠããŸãã ãµãŒããŒã³ãŒãã
src/server/server.js
å ¥ããŸãããïŒ
const path = require('path') module.exports = async function(fastify, opts) { // `src/ui` fastify.register(require('fastify-static'), { root: path.join(__dirname, '..', 'ui'), }) // API fastify.get('/api/time', async (request, reply) => { return { time: new Date().toISOString() } }) }
äžèšã®ã³ãŒãã¯ããèªäœããã説æããŠãããšæããŸãããããã€ãã®èå³æ·±ã詳现ããããŸãã ããã¯ãFastifyã®çµéšããªã人ã«ç¹ã«åœ¹ç«ã¡ãŸãã
ãã®ã³ãŒãã§æåã«æ³šç®ã§ããããšã¯ãããã§
async
ããŒã¯ãŒãã䜿çšãããŠããããšã§ãã Fastifyã¯ãéåæ/åŸ æ©ã¹ã¿ã€ã«ã®éçºãšåŸæ¥ã®ã³ãŒã«ããã¯ã¢ãããŒãã®äž¡æ¹ããµããŒãããŠããŸãã æ£ç¢ºã«éžæãããã®ã¯ãç¹å®ã®éçºè ã®å¥œã¿ã«äŸåããŸãã
å¥ã®èå³æ·±ã詳现ã¯ãããã§ãµãŒããŒããšã¯ã¹ããŒããããã¢ãžã¥ãŒã«ãšããŠå®çŸ©ããããšã§ãã ãã®ã¢ãžã¥ãŒã«ïŒFastifyã®å°éçšèªã§ã¯ããã©ã°ã€ã³ããšåŒã°ããŸãïŒã¯ãFastifyã€ã³ã¹ã¿ã³ã¹ïŒ
fastify
ïŒãšãªãã·ã§ã³ã»ããïŒ
opts
ïŒãåŒæ°ãšããŠåãé¢æ°ã§ãã ã¢ãžã¥ãŒã«å®£èšå ã§ã
fastify
ã€ã³ã¹ã¿ã³ã¹ã䜿çšããŠãã©ã°ã€ã³ãç»é²ã§ããŸãã ããã¯ã
fastify-static
ãã©ã°ã€ã³ã§èµ·ããããšã§ãã
fastify.get
ã
fastify.post
ãªã©ã®ç¹å¥ãªã¡ãœããã䜿çšããŠãHTTPãšã³ããã€ã³ããèšè¿°ããããšãã§ããŸãã
ããã§äœ¿çšãããã¢ãžã¥ã©ãŒã¢ãããŒãã¯ãå°ãçããããã«èŠããŸãããå©ç¹ããããŸãã ãŸããè€æ°ã®ãµãŒããŒãçµã¿åãããããšãã§ããããšã«æ³šæããŠãã ããã ããã°çšã«èšèšããããµãŒããŒãšããã©ãŒã©ã çšã«å¥ã®ãµãŒããŒãäœæãããšæ³åããŠãã ããã
/blog
ã
/forum
ãªã©ã®ãã¹ã«æ·»ä»ããããšã§ãæ¢åã®ã¢ããªã±ãŒã·ã§ã³ã«ç°¡åã«çµ±åã§ããŸãã
ããã«ããã®ã¢ãããŒãã«ããããµãŒããŒãã€ã³ãã£ã³ã°ïŒãœã±ãããã€ã³ãã£ã³ã°ãªã©ïŒããã¢ããªã±ãŒã·ã§ã³ãšãµãã¢ããªã±ãŒã·ã§ã³ãæœè±¡åãããã®åé¡ã®è§£æ±ºçãã«ãŒãã¢ããªã±ãŒã·ã§ã³ãŸãã¯
fastify-cli
ããšãã§ããŸãã
fastify
ã³ãã³ãã©ã€ã³
fastify
ã䜿çšããŠãµãŒããŒãèµ·åããŸãã
node_modules/.bin/fastify start --log-level info src/server/server.js
ç§ãã¡ã®ç掻ãç°¡çŽ åããããã«ã
package.json
ãã¡ã€ã«ã®
scripts
ã»ã¯ã·ã§ã³ã«æ¬¡ã®ã³ãã³ããè¿œå ã§ããŸãã
{ "scripts": { "start": "fastify start --log-level info src/server/server.js" } }
ãµãŒããŒãå®éã«èµ·åããåã«ãéçãªãœãŒã¹ãé 眮ããããã©ã«ããŒãããããšã確èªããå¿ èŠããããŸãã ãã以å€ã®å Žåã
fastify-static
ã¯ãšã©ãŒã
fastify-static
ãŸãã ãã®ãã©ã«ããŒãäœæããŸãã
mkdir src/ui
ããã§ã
npm start
ã³ãã³ãã§ã¢ããªã±ãŒã·ã§ã³ã
npm start
ãããã©ãŠã¶ãŒã䜿çšããŠ
localhost:3000/api/time
ãŸãã
ãã¹ãŠãæ£åžžã«æ©èœããå Žåããã©ãŠã¶ã§æ¬¡ã®ãããªãã®ã衚瀺ãããŸãã
{ "time": "2019-02-17T19:32:03.354Z" }
ãã®æç¹ã§ãFastifyã®å¥ã®åªããæ©èœãç解ã§ããŸãã ç¹å®ã®ã«ãŒãããªããžã§ã¯ããè¿ãå ŽåãJSONã·ãªã¢ã«åãèªåçã«é©çšããããšããäºå®ã«ãããŸãã
ããã§ããµãŒããŒAPIã§ã®äœæ¥ãå®äºããŸããã ããã³ããšã³ããçšæããŸãããã
ããã³ããšã³ãã®ã»ããã¢ãã
ããã³ããšã³ãã«é¢é£ãããããžã§ã¯ãã®ãã¹ãŠã®ã³ãŒãã¯ã
src/ui
ãã©ã«ããŒã«ãããŸãã 5ã€ã®ãã¡ã€ã«ã§æ§æãããŸãã
-
app.js
æ£ç¢ºãªã¢ããªã±ãŒã·ã§ã³ã³ãŒãã -
bootstrap.min.css
ã¢ããªã±ãŒã·ã§ã³ã®ã¹ã¿ã€ã«ãèšå®ããããã®CSSã³ãŒãïŒBootstrapãã¬ãŒã ã¯ãŒã¯ããçŽæ¥ååŸãããŸãïŒã -
favicon.ico
ãã¡ãã³ã³ãã¡ã€ã«ã æ¬æ Œçãªã¢ããªã±ãŒã·ã§ã³ãéçºããŠããå Žåãé©åãªãã¡ãã³ã³ãã¡ã€ã«ããªããšå®è¡ã§ããŸããã -
index.html
ã¯ã1ããŒãžã¢ããªã±ãŒã·ã§ã³ã®ã¡ã€ã³HTMLãã¡ã€ã«ã§ãã -
preacthtm.js
-Preactããã³htmã©ã€ãã©ãªã®ã³ãŒãã
éå§ããã«ã¯ãã¹ã¿ã€ã«ãã©ã€ãã©ãªãããã³ãã¡ãã³ã³ã¢ã€ã³ã³ã§ãããã©ã«ããŒã«ãã¡ã€ã«ãé 眮ããŸãã
curl "https://unpkg.com/htm@2.0.0/preact/standalone.js" > src/ui/preacthtm.js curl "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" > src/ui/bootstrap.min.css curl "https://github.com/lmammino/fastify-preact-htm-boilerplate/blob/master/src/ui/favicon.ico?raw=true" > src/ui/favicon.ico
次ã«ã
src/ui/index.html
ãã¡ã€ã«ãäœæããŸãã
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <!-- Bootstrap CSS --> <link rel="stylesheet" href="/bootstrap.min.css" /> <title>My awesome server time</title> </head> <body> <div id="app"></div> <!-- JavaScript --> <script src="/preacthtm.js"></script> <script src="/app.js"></script> </body> </html>
ãã¹ãŠã®ãªãœãŒã¹ïŒCSSããã³JSïŒãããŒããã
app
IDãæã€ç©ºã®
<div>
èŠçŽ ãäœæããéåžžã«æ®éã®HTMLããŒãžãåã«ããããããžã§ã¯ãã®å®è¡äžã«ã¢ããªã±ãŒã·ã§ã³ãåºåããŸãã
次ã«ã
src/ui/app.js
ã«ããã¯ãã®ã¢ããªã±ãŒã·ã§ã³ã³ãŒããèŠãŠã¿ãŸãããã
/* htmPreact */ const { html, Component, render } = htmPreact class App extends Component { componentDidMount() { this.setState({ loading: true, time: null }) fetch('/api/time') .then(response => response.json()) .then(data => this.setState({ loading: false, time: data.time })) } render(props, state) { return html` <div class="container mt-5"> <div class="row justify-content-center"> <div class="col"> <h1>Hello from your new App</h1> <div> ${state.loading && html` <p>Loading time from server...</p> `} ${state.time && html` <p>Time from server: <i><font color="#999999">${state.time}</font></i> </p> `} </div> <hr /> <div> Have fun changing the code from this boilerplate: <ul> <li>UI code available at <code>/src/ui</code></li> <li>Server-side code available at <code>/src/server</code></li> </ul> </div> </div> </div> </div> ` } } render( html` <${App} /> `, document.getElementById('app') )
ãã®ã¢ããªã±ãŒã·ã§ã³ã«ã¯
App
ãšåŒã°ããã¹ããŒããã«ã³ã³ããŒãã³ãã1ã€ã ããããŸãã ãã®ã³ã³ããŒãã³ãã®ç¶æ ã«ã¯ã2ã€ã®å€æ°ãå«ãŸããŸãã
-
loading
ã¯ããµãŒããŒæéã«é¢ããæ å ±ãååŸããããã«ãç¹å®ã®æç¹ã§ãµãŒããŒAPIãžã®èŠæ±ãå®è¡ãããŠãããã©ããã瀺ãããã«äœ¿çšãããè«çå€æ°ã§ãã - time-ãµãŒããŒããåä¿¡ããææ°ã®æå»æ å ±ãå«ãæååã
Reactã«ç²ŸéããŠããã°ãäžèšã®ã³ãŒããç°¡åã«ç解ã§ããŸãã
Preactãšhtmã䜿çšããŠãçµã¿èŸŒã¿ã®
Component
ã¯ã©ã¹ãæ¡åŒµããã¯ã©ã¹ã宣èšããããšã«ãããã³ã³ããŒãã³ããäœæã§ããŸãã
ãã®ã¯ã©ã¹ã§ã¯ã
componentDidMount()
ãªã©ã®ã©ã€ããµã€ã¯ã«ã¡ãœããã䜿çšããŠã³ã³ããŒãã³ãã®åäœãèšè¿°ã§ããReactã®éåžžã®
render()
ã¡ãœããã®ããã«åäœããã¡ãœããã䜿çšã§ã
render()
ã
ãã®å Žåãã³ã³ããŒãã³ããããŒãžã«æ¥ç¶ããããšããã«ïŒ
componentDidMount()
ã¡ãœããïŒãã¹ããŒã¿ã¹ã®
loading
ããããã£ãèšå®ãã
fetch
ã䜿çšããŠAPIãªã¯ãšã¹ããå®è¡ããŸãã
ãªã¯ãšã¹ããå®äºããåŸã
time
stateããããã£ã®å€ãèšå®ãã
loading
ããããã£ã
false
ãªã»ããã
false
ã
render()
ã¡ãœããã¯ãã³ã³ããŒãã³ãã®ç¶æ ãå€ãããã³ã«ããŸãã¯æ°ããããããã£ãæž¡ããããã³ã«èªåçã«åŒã³åºãããŸãã ãã®ã¡ãœããã§ã¯ãhtmã䜿çšããŠDOMã³ã³ããŒãã³ãã説æããŸãã
htmã©ã€ãã©ãªã§ã¯ãã¿ã°ä»ããã³ãã¬ãŒããªãã©ã«ãšç¹å¥ãªã¿ã°
html
ã䜿çšããŠDOMããŒããèšè¿°ããããšãã§ããŸãã ãã³ãã¬ãŒããªãã©ã«å ã«ã¯ãã¹ããŒã¿ã¹ã確èªããã¢ããªã±ãŒã·ã§ã³ããµãŒããŒããããŒã¿ãèªã¿èŸŒãã§ããå Žåãããã³ããŒã¿ãæ¢ã«ååšããå Žåã«è¡šç€ºãããã®ã決å®ããããã«äœ¿çšããåŒã®ãããªåçåŒãååšããå ŽåããããŸãããŒããããŸããã
ãŸããã¢ããªã±ãŒã·ã§ã³ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããHTMLããŒãžã«è¡šç€ºããå¿ èŠãããããšã«ã泚æããŠãã ããã ããã¯ã
htmPreact
ã°ããŒãã«ãªããžã§ã¯ãã®
render()
é¢æ°ã䜿çšããŠè¡ãããŸãã
ããã§ãããã³ããšã³ãã¢ããªã±ãŒã·ã§ã³ã®äœæ¥ãå®äºããŸããã ãµãŒããŒãåèµ·åãã
localhost:3000
移åããŠãäœæãããã®ãè©Šãããšãã§ããŸãã ããšãã°ããã®ã¢ããªã±ãŒã·ã§ã³ã«åºã¥ããŠç¬èªã«éçºã§ããŸãã ãããŠãããªããæ§ç¯ãããã®ãä»ã®èª°ãã«ããã瀺ãã®ã«ååèå³æ·±ããšæããããšããããããããªãã¯ããªãã®ã¢ããªã±ãŒã·ã§ã³ãDockerã³ã³ããã«è©°ã蟌ãã®ã«åœ¹ç«ã€ã§ãããã
ã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ããå
æ°ããå°ããªãããžã§ã¯ããä»ã®äººã«èŠããããã®æè¯ã®æ¹æ³ã¯ããã®ç®çã®ããã«Dockerã®æ©èœã䜿çšããããšã ãšæããŸãã
Dockerã®ãããã§ãèªå® ã§ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããããšãã人ã¯ãé©åãªããŒãžã§ã³ã®Node.jsãšNPMãã€ã³ã¹ããŒã«ãããŠãããã©ãããèããå¿ èŠããªããªããŸããæ£ããã³ãã³ãã·ãŒã±ã³ã¹ãå ¥åããããšã§ãã¢ããªã±ãŒã·ã§ã³ã®ãœãŒã¹ã³ãŒããããŠã³ããŒãããå¿ èŠã¯ãããŸãããäŸåé¢ä¿ãã€ã³ã¹ããŒã«ããŠãµãŒããŒãèµ·åããŸãã
ã¢ããªã±ãŒã·ã§ã³ãDockerã³ã³ãããŒã«ããã¯ããã«ã¯ããããžã§ã¯ãã®ã«ãŒããã©ã«ããŒã«éåžžã«åçŽãª
Dockerfile
ãã¡ã€ã«ãäœæããå¿ èŠããããŸãã
FROM node:11-alpine WORKDIR /app COPY . /app RUN npm install --production EXPOSE 3000 CMD ["npm", "start"]
ããã§ã¯ã次ã®ã¢ã¯ã·ã§ã³ã«ã€ããŠèª¬æããŸãã
- ã€ã¡ãŒãžã¯ãAlpine Linuxã«åºã¥ããŠæ§ç¯ãããNode.js 11ã€ã¡ãŒãžã«åºã¥ããŠäœæãããŸãã
- çŸåšã®ãã©ã«ããŒã®ãã¹ãŠãã³ã³ãããŒ
/app
ãã©ã«ããŒã«ã³ããŒãããŸãã - ãã®åŸã
npm install
ã³ãã³ããå®è¡ããŠãäŸåé¢ä¿ãããŠã³ããŒãããŠã€ã³ã¹ããŒã«ããŸãã--production
ãã©ã°ã䜿çšãããšã--production
ã§ã®ãããžã§ã¯ãã®ãããã€ã¡ã³ãã«å¿ èŠãªäŸåé¢ä¿ã®ã¿ãã€ã³ã¹ããŒã«ããããšããäºå®ã«ã€ãªãããŸãã ãããžã§ã¯ããå€ãã®éçºäŸåé¢ä¿ã䜿çšããå Žåãããã«ããã€ã¡ãŒãžã®äœæãé«éåãããŸãã - ã³ã³ããã«ã¯ãããã©ã«ãã§ãµãŒããŒãæ©èœãããªãŒãã³ãã¢3000ãå¿ èŠã§ããããšã瀺ããŸãã
- æåŸã«ãã³ã³ããã®èµ·åæã«å®è¡ãããã³ãã³ã
npm start
ã«ã€ããŠèª¬æããŸãã 圌女ã¯ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸãã
ã³ã³ããã®ã€ã¡ãŒãžãåéããã«ã¯ã次ã®ã³ãã³ããå®è¡ããŸãã
docker build -t server-time .
æ°ç§åŸãã€ã¡ãŒãžã®æºåãæŽããã³ã³ãããŒãéå§ã§ããããã«ãªããŸãã
docker run -it -p 3000:3000 server-time
-p
ãã©ã¡ãŒã¿ãŒã䜿çšãããšãã³ã³ãããŒããŒã3000ãšããŒã«ã«ããŒã3000ã®éã®æ¥ç¶ãæ§æã§ããŸããããã«ããã
localhost:3000
ã³ã³ãããŒåãããã¢ããªã±ãŒã·ã§ã³ã«ã¢ã¯ã»ã¹ã§ããŸãã
ããã§ãã¢ããªã±ãŒã·ã§ã³ãä»ã®äººãšå ±æããæºåãã§ããŸããã Dockerç°å¢ã§å®è¡ããã«ã¯ãDockerãã³ã³ãã¥ãŒã¿ãŒã«ã€ã³ã¹ããŒã«ãããŠããå Žåããã®ãã©ã«ããŒã§äžèšã®2ã€ã®ã³ãã³ããå®è¡ããã ãã§ååã§ãã
ãŸãšã
ãã®èšäºã§ã¯ãFastifyããã³Preactã䜿çšããŠWebã¢ããªã±ãŒã·ã§ã³ãè¿ éã«éçºããããã®ç°å¢ãäœæããæ¹æ³ã«ã€ããŠèª¬æããŸããã ããã«ãDockerã䜿çšããŠä»ã®ãŠãŒã¶ãŒãšã¢ããªã±ãŒã·ã§ã³ãå ±æããæ¹æ³ã«ã€ããŠã話ããŸããã
åè¿°ã®ããã«ãææ¡ãããããŒã«ã¯ã©ããããããã¿ã€ãã³ã°çšã«èšèšãããŠãããããå®éã®ã¢ããªã±ãŒã·ã§ã³ãéçºããããã«ããã«äœãæ¬ ããŠããã®ãçåã«æããããããŸããã ã»ãšãã©ã®å Žåããå®éã®ã¢ããªã±ãŒã·ã§ã³ããšèšãã°ã次ã®æ©èœãæå³ããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®ããã³ããšã³ãéšåã®ãªãœãŒã¹ã®ã³ã³ãã€ã«ïŒæé©åããããã¡ã€ã«ïŒãã³ãã«ïŒã®äœæãããããWebpackãBabelããŸãã¯ãã®ä»ã®ããŒã«ã䜿çšããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã®ããã³ããšã³ãã§ã®ã«ãŒãã£ã³ã°ã
- ãµãŒããŒã¬ã³ããªã³ã°
- æ°žç¶çãªããŒã¿ã¹ãã¬ãŒãžã®æ段ã
å®éã®ã¢ããªã±ãŒã·ã§ã³ãéçºããããã®ãããã®å¯èœæ§ã¯ãã¹ãŠãããã§èª¬æããäžé£ã®ãã¯ãããžãŒã«ãŸã è¿œå ãããŠããªããããä»ã®ãšããããããã¿ã€ããéçºããããã®ããŒã«ãšããŠèããŠããŸãã ããªããèŠããã®ã奜ãã§ãããå®éã®åé¡ã解決ããå°æ¥ã®ã¢ããªã±ãŒã·ã§ã³ã®åºç€ãšããŠããããã¹ãŠãèæ ®ããã°ãããªãã¯å¿ èŠãªãã®ãç°¡åã«èŠã€ããFastifyãšPreactã§äœ¿çšã§ããã¢ããªã±ãŒã·ã§ã³ãäœæã§ãããšç¢ºä¿¡ããŠããŸãçç£ãªãªãŒã¹ã
芪æãªãèªè ïŒ Webã¢ããªã±ãŒã·ã§ã³ãã©ã®ããã«ãããã¿ã€ãåããŸããïŒ