{"id":1479,"date":"2015-01-13T01:50:20","date_gmt":"2015-01-12T16:50:20","guid":{"rendered":"http:\/\/tech.akat.info\/?p=1479"},"modified":"2015-01-13T01:50:20","modified_gmt":"2015-01-12T16:50:20","slug":"node-js%e3%81%84%e3%82%8d%e3%81%84%e3%82%8d%e3%82%84%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=1479","title":{"rendered":"Node.js\u3044\u308d\u3044\u308d\u3084\u3063\u3066\u307f\u305f"},"content":{"rendered":"<h1>sys<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# cat sys_sample.js\r\nvar sys = require('sys');\r\nvar obj = {\r\n        name: &quot;shimizu&quot;,\r\n        age: 21,\r\n};\r\n\r\nsys.print(&quot;Print message\\n&quot;);\r\nsys.log('Log message.');\r\nsys.print(sys.inspect(obj)+&quot;\\n&quot;);\r\n\r\nroot@hostname:\/home\/shimizu\/nodejs# nodejs sys_sample.js\r\nPrint message\r\n12 Jan 23:53:01 - Log message.\r\n{ name: 'shimizu', age: 21 }\r\n<\/pre>\n<h1>jsdom<\/h1>\n<p>JavaScript\u306b\u3088\u308bDOM\u89e3\u6790\u3068\u64cd\u4f5c\u3092\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9(nodejs)\u3067\u5b9f\u65bd\u3067\u304d\u308b<\/p>\n<h1>sample1<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs# cat hello_dom.js\r\nvar jsdom    = require(&quot;jsdom&quot;).jsdom;\r\nvar document = jsdom(&quot;&lt;html&gt;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;&lt;div id='greeting'&gt;hello world&lt;\/div&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;);\r\nvar window = document.parentWindow;\r\nconsole.log(window.document.getElementById(&quot;greeting&quot;).innerHTML);\r\nroot@hostname:\/home\/shimizu\/nodejs# nodejs hello_dom.js\r\nhello world\r\n<\/pre>\n<h1>sample2<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler1# cat html.js\r\nvar jsdom    = require(&quot;jsdom&quot;).jsdom,\r\n    document = jsdom(&quot;&lt;html&gt;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;hello world&lt;\/body&gt;&lt;\/html&gt;&quot;),\r\n    window   = document.createWindow();\r\n\r\nconsole.log(window.document.innerHTML);\r\n\/\/ output: '&lt;html&gt;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;hello world&lt;\/body&gt;&lt;\/html&gt;'\r\n\r\nconsole.log(window.innerWidth)\r\n\/\/ output: 1024\r\n\r\nconsole.log(typeof window.document.getElementsByClassName);\r\n\/\/ outputs: function\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler1# nodejs html.js\r\n\r\n\/home\/shimizu\/nodejs\/crawler1\/html.js:3\r\n    window   = document.createWindow();\r\n                        ^\r\nTypeError: Object &#x5B;object HTMLDocument] has no method 'createWindow'\r\n    at Object.&lt;anonymous&gt; (\/home\/shimizu\/nodejs\/crawler1\/html.js:3:25)\r\n    at Module._compile (module.js:456:26)\r\n    at Object.Module._extensions..js (module.js:474:10)\r\n    at Module.load (module.js:356:32)\r\n    at Function.Module._load (module.js:312:12)\r\n    at Function.Module.runMain (module.js:497:10)\r\n    at startup (node.js:119:16)\r\n    at node.js:906:3\r\n\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler1# cat html.js\r\nvar jsdom    = require(&quot;jsdom&quot;).jsdom,\r\n    document = jsdom(&quot;&lt;html&gt;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;hello world&lt;\/body&gt;&lt;\/html&gt;&quot;),\r\n    window   = document.parentWindow; \/\/ createWindow()\u3092\u4fee\u6b63\u3057\u305f\r\n\r\nconsole.log(window.document.innerHTML);\r\n\/\/ output: '&lt;html&gt;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;hello world&lt;\/body&gt;&lt;\/html&gt;'\r\n\r\nconsole.log(window.innerWidth)\r\n\/\/ output: 1024\r\n\r\nconsole.log(typeof window.document.getElementsByClassName);\r\n\/\/ outputs: function\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler1# nodejs html.js\r\nundefined\r\n1024\r\nfunction\r\n<\/pre>\n<h1>crawler\u306e\u4f5c\u6210<\/h1>\n<p>\u30bf\u30a4\u30c8\u30eb\u3092\u53d6\u5f97\u3057DB\u306b\u30a4\u30f3\u30b5\u30fc\u30c8\u3059\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler2# npm install request mysql sequelize cheerio\r\nroot@hostname:\/home\/shimizu\/nodejs\/crawler2# cat crawler.js\r\n\/\/ Initialize instance\r\nvar sequelize = require('sequelize');\r\n\r\n\/\/ Connect to database\r\nvar connection = new sequelize('DBName', 'user', 'password');\r\n\r\n\/\/ Define models\r\nvar sites = connection.define('sites', {\r\n    url: sequelize.TEXT,\r\n    title: sequelize.STRING\r\n});\r\n\r\n\/\/Initialize instance\r\nvar request = require(&quot;request&quot;);\r\nvar cheerio = require(&quot;cheerio&quot;);\r\n\r\n\/\/ Define request url\r\nvar requestUrl = &quot;http:\/\/www.google.co.jp&quot;;\r\n\r\n\/\/ Send http request\r\nrequest({url: requestUrl}, function(error, response, body) {\r\n\r\n    \/\/ If request succeed\r\n    if (!error &amp;&amp; response.statusCode == 200) {\r\n        $ = cheerio.load(body); \/\/ Create cheerio instance\r\n\r\n        \/\/ Get response data\r\n        var url = response.request.href;\r\n        var title = $(&quot;title&quot;).text();\r\n\r\n        console.log(url);\r\n        console.log(title);\r\n\r\n        \/\/ Create new instance\r\n        var site = sites.build();\r\n\r\n        \/\/ Set fields\r\n        site.url = url;\r\n        site.title = title;\r\n\r\n        \/\/ Save to database\r\n        site.save()\r\n            .success(function(anotherTask) {\r\n                console.log('Succeed');\r\n            })\r\n            .error(function(error) {\r\n                console.log(error);\r\n            });\r\n    }\r\n\r\n    \/\/ If error occured\r\n    else {\r\n        console.log(&quot;--------------------------------------------------&quot;);\r\n        if (error &amp;&amp; &quot;code&quot; in error) {\r\n            console.log(&quot;Error Code:&quot; + error.code);\r\n        }\r\n        if (error &amp;&amp; &quot;errno&quot; in error) {\r\n            console.log(&quot;Error No:&quot; + error.errno);\r\n        }\r\n        if (error &amp;&amp; &quot;syscall&quot; in error) {\r\n            console.log(&quot;Error Syscall:&quot; + error.syscall);\r\n        }\r\n        if (response &amp;&amp; &quot;statusCode&quot; in response) {\r\n            console.log(&quot;Status Code:&quot; +  response.statusCode);\r\n        }\r\n    }\r\n});\r\n\r\n<\/pre>\n<p>mysql\u5185\u3092\u78ba\u8a8d\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n*************************** 1. row ***************************\r\n       id: 1\r\n      url: http:\/\/www.google.co.jp\/\r\n    title: Google\r\ncreatedAt: 2015-01-12 16:04:14\r\nupdatedAt: 2015-01-12 16:04:14\r\n3 rows in set (0.00 sec)\r\n\r\n<\/pre>\n<h1>\u53c2\u8003URL<\/h1>\n<p>node.js\u3067\u904a\u3076: \u6a19\u6e96\u30e2\u30b8\u30e5\u30fc\u30eb\u7de8<br \/>\nhttp:\/\/blog.summerwind.jp\/archives\/1436<br \/>\nnode.js\u3092\u4f7f\u3063\u3066jQuery\u30c1\u30c3\u30af\u306bWeb\u30b5\u30a4\u30c8\u3092\u30af\u30ed\u30fc\u30ea\u30f3\u30b0\u3059\u308b\u65b9\u6cd5<br \/>\nhttp:\/\/liginc.co.jp\/programmer\/archives\/4848<br \/>\n\u30d0\u30fc\u30b8\u30e7\u30f3\u3054\u3068\u306edocs<br \/>\nhttp:\/\/nodejs.org\/docs\/<\/p>\n<p>http:\/\/sakuratan.biz\/archives\/3393<br \/>\nhttps:\/\/github.com\/tmpvar\/jsdom<br \/>\nhttp:\/\/blog.summerwind.jp\/archives\/1436<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sys root@hostname:\/home\/shimizu\/nodejs# cat sys_sample.js var sys = require(&#8216;sys&#8217;); var obj = { name: &quot;sh [&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\/1479"}],"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=1479"}],"version-history":[{"count":1,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/1479\/revisions\/1480"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}