{"id":2690,"date":"2013-03-26T11:22:37","date_gmt":"2013-03-26T02:22:37","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2690"},"modified":"2013-12-29T23:08:12","modified_gmt":"2013-12-29T14:08:12","slug":"post-2690","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/2690","title":{"rendered":"[node.js]\u9759\u7684\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u8868\u793a\u3059\u308bWEB\u30b5\u30fc\u30d0\u30fc"},"content":{"rendered":"<p>\u30ed\u30fc\u30ab\u30eb\u3067\u3061\u3087\u3063\u3068\u3057\u305f\u30c6\u30b9\u30c8\u3092\u884c\u3044\u305f\u3044\u3068\u304d\u306a\u3069\u306b\u3002<br \/>\n\u5b9f\u884c\u3059\u308b\u3068\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u30eb\u30fc\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3068\u3057\u305fWEB\u30b5\u30fc\u30d0\u30fc\u304c\u30ed\u30fc\u30ab\u30eb\u3067\u8d77\u52d5\u3057\u307e\u3059\u3002<br \/>\n\u4e0b\u4f4d\u30d5\u30a9\u30eb\u30c0\u306a\u3069\u306b\u7f6e\u304b\u308c\u305f\u30b3\u30f3\u30c6\u30f3\u30c4\u306b\u3082\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3064\u3044\u3067\u306bJSON\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u306b\u306f\u3001\u30d8\u30c3\u30c0\u30fc\u306b\u300cAccess-Control-Allow-Origin\u300d\u3092\u4ed8\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"server.js\" >var http = require(&quot;http&quot;),\r\n    url = require(&quot;url&quot;),\r\n    path = require(&quot;path&quot;),\r\n    fs = require(&quot;fs&quot;),\r\n    port = process.argv[2] || 8888;\r\n \r\n     \r\nhttp.createServer(function(request, response) {\r\n\r\n\tvar Response = {\r\n\t\t&quot;200&quot;:function(file, filename){\r\n\t\t\tvar extname = path.extname(filename);\r\n\t\t\tvar header = {\r\n\t\t\t\t&quot;Access-Control-Allow-Origin&quot;:&quot;*&quot;,\r\n\t\t\t\t&quot;Pragma&quot;: &quot;no-cache&quot;,\r\n\t\t\t\t&quot;Cache-Control&quot; : &quot;no-cache&quot;\t   \r\n\t\t\t}\r\n\r\n\t\t\tresponse.writeHead(200, header);\r\n\t\t\tresponse.write(file, &quot;binary&quot;);\r\n\t\t\tresponse.end();\r\n\t\t},\r\n\t\t&quot;404&quot;:function(){\r\n\t\t\tresponse.writeHead(404, {&quot;Content-Type&quot;: &quot;text\/plain&quot;});\r\n\t\t\tresponse.write(&quot;404 Not Found\\n&quot;);\r\n\t\t\tresponse.end();\r\n\t\t\r\n\t\t},\r\n\t\t&quot;500&quot;:function(err){\r\n\t\t\tresponse.writeHead(500, {&quot;Content-Type&quot;: &quot;text\/plain&quot;});\r\n\t\t\tresponse.write(err + &quot;\\n&quot;);\r\n\t\t\tresponse.end();\r\n\t\t\r\n\t\t}\r\n\t}\r\n\r\n  \r\n\tvar uri = url.parse(request.url).pathname\r\n\t, filename = path.join(process.cwd(), uri);\r\n\r\n\tfs.exists(filename, function(exists){\r\n\t\tconsole.log(filename+&quot; &quot;+exists);\r\n\t\tif (!exists) { Response[&quot;404&quot;](); return ; }\r\n\t\tif (fs.statSync(filename).isDirectory()) { filename += '\/index.html'; }\r\n\r\n\t\tfs.readFile(filename, &quot;binary&quot;, function(err, file){\r\n\t\tif (err) { Response[&quot;500&quot;](err); return ; }\r\n\t\t\tResponse[&quot;200&quot;](file, filename);   \r\n\t\t}); \r\n\r\n\t});\r\n \r\n   \r\n}).listen(parseInt(port, 10));\r\n  \r\nconsole.log(&quot;Server running at http:\/\/localhost:&quot; + port );<\/pre>\n<p>\u5b9f\u884c<\/p>\n<pre class=\"lang:sh decode:true \" >$ node server.js <\/pre>\n<p>D3.js\u306ecsv\u30e1\u30bd\u30c3\u30c9\u306a\u3093\u304b\u306f\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u95a2\u4fc2\u3067\u30ed\u30fc\u30ab\u30eb\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u3081\u306a\u3044(chrome\u306e\u8d77\u52d5\u30aa\u30d7\u30b7\u30e7\u30f3\u3068\u304b\u3067\u56de\u907f\u3067\u304d\u308b\u307f\u305f\u3044\u3060\u3051\u3069)\u306e\u3067\u3001\u30b7\u30f3\u30d7\u30eb\u306aweb\u30b5\u30fc\u30d0\u30fc\u304c\u3042\u308b\u3068\u91cd\u5b9d\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30ed\u30fc\u30ab\u30eb\u3067\u3061\u3087\u3063\u3068\u3057\u305f\u30c6\u30b9\u30c8\u3092\u884c\u3044\u305f\u3044\u3068\u304d\u306a\u3069\u306b\u3002&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2690","post","type-post","status-publish","format-standard","hentry","category-node-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2690"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2690\/revisions"}],"predecessor-version":[{"id":2691,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2690\/revisions\/2691"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}