{"id":1249,"date":"2014-11-24T02:30:20","date_gmt":"2014-11-23T17:30:20","guid":{"rendered":"http:\/\/tech.akat.info\/?p=1249"},"modified":"2014-12-17T23:48:57","modified_gmt":"2014-12-17T14:48:57","slug":"node-js","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=1249","title":{"rendered":"Node.js"},"content":{"rendered":"<h1>\u6982\u8981<\/h1>\n<p>Node.js\u306b\u3064\u3044\u3066\u3084\u3063\u3066\u307f\u305f<br \/>\nhttp:\/\/dotinstall.com\/lessons\/basic_nodejs<\/p>\n<h1>\u7279\u5fb4<\/h1>\n<p>\u5927\u91cf\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9ad8\u901f\u306b\u51e6\u7406\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd(API\u3084\u30e1\u30c3\u30bb\u30fc\u30b8\u30f3\u30b0\u90e8\u5206)<br \/>\nJavaScript\u3068\u540c\u3058\u6587\u6cd5\u3067\u8a18\u8f09\u3059\u308b<br \/>\n\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u63a1\u7528\u3057\u3066\u3044\u308b<\/p>\n<h2>\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3068\u306f<\/h2>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_011930.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_011930.png\" alt=\"2014-11-24_011930\" width=\"650\" height=\"436\" class=\"alignnone size-full wp-image-1250\" \/><\/a><br \/>\n\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9(Queue)\u3068\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9(I\/O)\u3067\u51e6\u7406\u3059\u308b\u30b9\u30ec\u30c3\u30c9\u304c\u5b58\u5728<br \/>\nrequest\u306fQueue\u306b\u683c\u7d0d\u3055\u308c\u3001EventLoop\u306b\u3088\u3063\u3066\u72b6\u614b\u3092\u5224\u65ad\u5f8c\u3001I\/O\u306b\u3088\u3063\u3066\u51e6\u7406\u3055\u308c\u308b<br \/>\n\u305d\u306e\u305f\u3081\u3001\u5927\u91cf\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u4ed8\u3051\u3066\u51e6\u7406\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd<br \/>\n\u30c7\u30e1\u30ea\u30c3\u30c8\u3068\u3057\u3066<br \/>\n\u30fb\u51e6\u7406\u304c\u7d42\u308f\u3063\u305f\u304b\u308f\u304b\u308a\u306b\u304f\u3044<br \/>\n\u30fb\u30eb\u30fc\u30d7\u3092\u30d6\u30ed\u30c3\u30af\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044(\u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u66f8\u304d\u65b9\u2192JavaScript\u306f\u3053\u306e\u6027\u8cea\u3092\u6301\u3063\u3066\u3044\u308b)<\/p>\n<h1>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@shimizu:\/home\/shimizu#  vi \/etc\/apt\/sources.list\r\n### \u4ee5\u4e0b\u3092\u8ffd\u8a18\u3059\u308b ###\r\ndeb http:\/\/ftp.jp.debian.org\/debian\/ wheezy-backports main contrib non-free\r\ndeb-src http:\/\/ftp.jp.debian.org\/debian\/ wheezy-backports main contrib non-free\r\n######################\r\n\r\nroot@shimizu:\/home\/shimizu# aptitude update \r\n...\r\n\r\nroot@shimizu:\/home\/shimizu# aptitude install nodejs nodejs-legacy\r\n\u4ee5\u4e0b\u306e\u65b0\u898f\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059:\r\n  libc-ares2{a} libv8-3.14.5{a} nodejs nodejs-legacy\r\n\u66f4\u65b0: 0 \u500b\u3001\u65b0\u898f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb: 4 \u500b\u3001\u524a\u9664: 0 \u500b\u3001\u4fdd\u7559: 16 \u500b\u3002\r\n2,657 k \u30d0\u30a4\u30c8\u306e\u30a2\u30fc\u30ab\u30a4\u30d6\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u5c55\u958b\u5f8c\u306b 7,425 k \u30d0\u30a4\u30c8\u306e\u30c7\u30a3\u30b9\u30af\u9818\u57df\u304c\u65b0\u305f\u306b\u6d88\u8cbb\u3055\u308c\u307e\u3059\u3002\r\n...\r\n\r\nroot@shimizu:\/home\/shimizu# nodejs --version\r\nv0.10.29\r\n<\/pre>\n<h1>\u5b9f\u884c\u3059\u308b<\/h1>\n<h2>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067\u5b9f\u884c<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# node\r\n&gt; console.log(&quot;hello world&quot;);\r\nhello world\r\nundefined\r\n&gt; .exit\r\n<\/pre>\n<h2>\u30d5\u30a1\u30a4\u30eb\u3067\u5b9f\u884c<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# cat hello.js\r\nconsole.log(&quot;hello world&quot;);\r\nroot@shimizu:\/home\/shimizu\/nodejs# node hello.js\r\nhello world\r\n<\/pre>\n<h2>\u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u51e6\u7406\u3068\u306f<\/h2>\n<p>setTimeout\u3084DB\u63a5\u7d9a\u306a\u3069\u306f\u6b21\u306e\u547d\u4ee4\u3092\u30d6\u30ed\u30c3\u30af\u3057\u306a\u3044\u3088\u3046\u306b\u304b\u304b\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044<br \/>\n\u4ee5\u4e0b\u306f\u5f8c\u306b\u66f8\u3044\u305fworld\u304c\u5148\u306b\u8868\u793a\u3055\u308c\u308b<\/p>\n<pre class=\"brush: plain; title: \u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u51e6\u7406; notranslate\" title=\"\u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u51e6\u7406\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# cat nonblocking.js\r\nsetTimeout(function() {\r\n        console.log(&quot;hello&quot;);\r\n},1000);\r\nconsole.log(&quot;world&quot;);\r\nroot@shimizu:\/home\/shimizu\/nodejs# node nonblocking.js\r\nworld\r\nhello\r\n<\/pre>\n<p>\u4ee5\u4e0b\u306fwhile\u6587\u304c\u7d42\u308f\u308b\u307e\u3067\u6b21\u306e\u547d\u4ee4\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044\u3001\u3088\u308d\u3057\u304f\u306a\u3044<\/p>\n<pre class=\"brush: plain; title: \u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u51e6\u7406; notranslate\" title=\"\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u306a\u51e6\u7406\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# cat bloking.js\r\nvar start = new Date().getTime();\r\nwhile (new Date().getTime() &lt; start + 1000);\r\nconsole.log(&quot;world&quot;);\r\nroot@shimizu:\/home\/shimizu\/nodejs# node bloking.js\r\nworld\r\n<\/pre>\n<h1>web\u30b5\u30fc\u30d0\u3092\u4f5c\u6210\u3059\u308b<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# cat server.js\r\nvar http = require('http');\r\nvar server = http.createServer();\r\nserver.on('request', function(req, res){\r\n        res.writeHead(200, {'Content-Type':'text\/plain'});\r\n        res.write('hello world');\r\n        res.end();\r\n});\r\nserver.listen(9200,'49.212.204.46');\r\nconsole.log(&quot;server listening...&quot;)\r\n\r\nroot@shimizu:\/home\/shimizu\/nodejs# node server.js\r\nserver listening...\r\n<\/pre>\n<p>\u5916\u90e8\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<br \/>\n<a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_015210.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_015210.png\" alt=\"2014-11-24_015210\" width=\"571\" height=\"94\" class=\"alignnone size-full wp-image-1251\" \/><\/a><\/p>\n<pre class=\"brush: plain; title: URL\u306b\u3088\u3063\u3066\u51e6\u7406\u3092\u5909\u66f4\u3059\u308b; notranslate\" title=\"URL\u306b\u3088\u3063\u3066\u51e6\u7406\u3092\u5909\u66f4\u3059\u308b\">\r\nroot@shimizu:\/home\/shimizu\/nodejs# cat server.js\r\nvar http = require('http');\r\nvar server = http.createServer();\r\nserver.on('request', function(req, res){\r\n        switch (req.url){\r\n                case '\/about':\r\n                        msg = &quot;about this page&quot;;\r\n                        break;\r\n\r\n                case '\/profile':\r\n                        msg = &quot;about me&quot;;\r\n                        break;\r\n\r\n                default:\r\n                        msg = &quot;wrong page&quot;;\r\n                        break;\r\n        }\r\n        res.writeHead(200, {'Content-Type':'text\/plain'});\r\n        res.write(msg);\r\n        res.end();\r\n});\r\nserver.listen(9200,'49.212.204.46');\r\nconsole.log(&quot;server listening...&quot;)\r\n\r\nroot@shimizu:\/home\/shimizu\/nodejs# node server.js\r\nserver listening...\r\n<\/pre>\n<p>\u5916\u90e8\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<br \/>\n<a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_022059.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-11-24_022059.png\" alt=\"2014-11-24_022059\" width=\"554\" height=\"98\" class=\"alignnone size-full wp-image-1252\" \/><\/a><\/p>\n<p>html\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# cat hello.html\r\n&lt;html&gt;\r\n&lt;h1&gt;Hello&lt;\/h1&gt;\r\n&lt;\/html&gt;\r\nroot@hostname:\/home\/shimizu\/nodejs# cat server.js\r\nvar http = require('http');\r\n        fs = require('fs'); # \u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u7528\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\r\nvar server = http.createServer();\r\nserver.on('request', function(req, res){\r\n        fs.readFile(__dirname + '\/hello.html' , 'utf-8' , function(err,data) {\r\n                if (err) {\r\n                        res.writeHead(404, {'Content-Type':'text\/plain'});\r\n                        res.write(&quot;not found!&quot;);\r\n                        return res.end();\r\n                }\r\n        res.writeHead(200, {'Content-Type':'text\/html'});\r\n        res.write(data);\r\n        res.end();\r\n        });\r\n});\r\nserver.listen(9200,'49.212.204.46');\r\nconsole.log(&quot;server listening...&quot;)\r\n\r\n<\/pre>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-12-17_202731.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-12-17_202731.png\" alt=\"2014-12-17_202731\" width=\"513\" height=\"151\" class=\"alignnone size-full wp-image-1307\" \/><\/a><\/p>\n<p>npm(node package manager)\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# curl -L https:\/\/npmjs.org\/install.sh | sh\r\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\r\n                                 Dload  Upload   Total   Spent    Left  Speed\r\n100   193  100   193    0     0    277      0 --:--:-- --:--:-- --:--:--   366\r\n100  6239  100  6239    0     0   4722      0  0:00:01  0:00:01 --:--:-- 10610\r\ntar=\/bin\/tar\r\nversion:\r\ntar (GNU tar) 1.26\r\nCopyright (C) 2011 Free Software Foundation, Inc.\r\n\u4f7f\u7528\u8a31\u8afe GPLv3+: GNU GPL version 3 \u307e\u305f\u306f\u305d\u308c\u4ee5\u964d &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\r\nThis is free software: you are free to change and redistribute it.\r\nThere is NO WARRANTY, to the extent permitted by law.\r\n\r\n&#x5B;\u53c2\u8003\u8a33]\r\n\u3053\u308c\u306f\u30d5\u30ea\u30fc\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u3067\u3059. \u5909\u66f4\u3068\u518d\u914d\u5e03\u306f\u81ea\u7531\u3067\u3059.\r\n\u6cd5\u5f8b\u3067\u8a8d\u3081\u3089\u308c\u308b\u7bc4\u56f2\u3067\u300c\u7121\u4fdd\u8a3c\u300d\u3067\u3059.\r\n\r\n\u4f5c\u8005: John Gilmore, Jay Fenlason.\r\ninstall npm@latest\r\nfetching: http:\/\/registry.npmjs.org\/npm\/-\/npm-2.1.12.tgz\r\n\/usr\/bin\/npm -&gt; \/usr\/lib\/node_modules\/npm\/bin\/npm-cli.js\r\nnpm@2.1.12 \/usr\/lib\/node_modules\/npm\r\nIt worked\r\n\r\nroot@hostname:\/home\/shimizu\/nodejs# npm install ejs\r\nejs@1.0.0 node_modules\/ejs\r\n\r\n<\/pre>\n<h1>Express\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5229\u7528\u3057\u3066\u307f\u308b<\/h1>\n<p>Node.js\u306e\u8efd\u91cf\u3001\u9ad8\u901f\u306aWEB\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af<\/p>\n<pre class=\"brush: plain; title: \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb; notranslate\" title=\"\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\">\r\nroot@hostname:\/home\/shimizu\/nodejs# npm install express\r\nexpress@4.10.6 node_modules\/express\r\n\u251c\u2500\u2500 utils-merge@1.0.0\r\n\u251c\u2500\u2500 merge-descriptors@0.0.2\r\n\u251c\u2500\u2500 fresh@0.2.4\r\n\u251c\u2500\u2500 cookie@0.1.2\r\n\u251c\u2500\u2500 escape-html@1.0.1\r\n\u251c\u2500\u2500 range-parser@1.0.2\r\n\u251c\u2500\u2500 cookie-signature@1.0.5\r\n\u251c\u2500\u2500 finalhandler@0.3.2\r\n\u251c\u2500\u2500 vary@1.0.0\r\n\u251c\u2500\u2500 media-typer@0.3.0\r\n\u251c\u2500\u2500 methods@1.1.0\r\n\u251c\u2500\u2500 parseurl@1.3.0\r\n\u251c\u2500\u2500 serve-static@1.7.1\r\n\u251c\u2500\u2500 content-disposition@0.5.0\r\n\u251c\u2500\u2500 path-to-regexp@0.1.3\r\n\u251c\u2500\u2500 depd@1.0.0\r\n\u251c\u2500\u2500 qs@2.3.3\r\n\u251c\u2500\u2500 on-finished@2.1.1 (ee-first@1.1.0)\r\n\u251c\u2500\u2500 debug@2.1.0 (ms@0.6.2)\r\n\u251c\u2500\u2500 etag@1.5.1 (crc@3.2.1)\r\n\u251c\u2500\u2500 send@0.10.1 (destroy@1.0.3, ms@0.6.2, mime@1.2.11)\r\n\u251c\u2500\u2500 proxy-addr@1.0.4 (forwarded@0.1.0, ipaddr.js@0.1.5)\r\n\u251c\u2500\u2500 accepts@1.1.4 (negotiator@0.4.9, mime-types@2.0.4)\r\n\u2514\u2500\u2500 type-is@1.5.4 (mime-types@2.0.4)\r\n<\/pre>\n<h2>hello world<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# cat app.js\r\nvar express = require('express')\r\nvar app = express()\r\n\r\napp.get('\/', function(req, res){\r\n        res.send('hello world');\r\n    });\r\n\r\napp.listen(3000);\r\nroot@hostname:\/home\/shimizu\/nodejs# node app.js\r\n\r\n<\/pre>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-12-17_223407.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/11\/2014-12-17_223407.png\" alt=\"2014-12-17_223407\" width=\"560\" height=\"180\" class=\"alignnone size-full wp-image-1308\" \/><\/a><\/p>\n<h2>set()\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u3066\u8a2d\u5b9a\u53ef\u80fd<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# cat app.js\r\nvar express = require('express')\r\nvar app = express()\r\napp.set('views','test');\r\nconsole.log(app)\r\nroot@hostname:\/home\/shimizu\/nodejs# node app.js | grep views\r\n     views: 'test',\r\n        views: 'test',\r\n<\/pre>\n<h1>\u53c2\u8003URL<\/h1>\n<p>http:\/\/nodejs.org\/<br \/>\nhttp:\/\/dotinstall.com\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 Node.js\u306b\u3064\u3044\u3066\u3084\u3063\u3066\u307f\u305f http:\/\/dotinstall.com\/lessons\/basic_nodejs \u7279\u5fb4 \u5927\u91cf\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9ad8\u901f\u306b\u51e6\u7406\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd(API\u3084\u30e1\u30c3\u30bb\u30fc\u30b8\u30f3\u30b0\u90e8\u5206) JavaScr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[45],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/1249"}],"collection":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1249"}],"version-history":[{"count":10,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":1313,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/1249\/revisions\/1313"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}