{"id":3975,"date":"2023-07-10T14:51:09","date_gmt":"2023-07-10T05:51:09","guid":{"rendered":"https:\/\/attacktube.com\/?p=3975"},"modified":"2023-07-26T17:54:02","modified_gmt":"2023-07-26T08:54:02","slug":"%e3%80%90node-js%e3%80%91fs-readfile","status":"publish","type":"post","link":"https:\/\/attacktube.com\/?p=3975","title":{"rendered":"\u3010Node.js\u3011fs.readFile"},"content":{"rendered":"\n<p>fs.readFile\u95a2\u6570\u306f,\u975e\u540c\u671f\u7684\u306bhtml,css,js\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<br>\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-3461056110605997\" crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3461056110605997\" data-ad-slot=\"3137443461\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<br>\n\n\n\n<p>main.js<\/p>\n\n\n\n<pre><code>\nvar http = require(&#039;http&#039;);\nvar fs = require(&#039;fs&#039;);\n\nhttp.createServer(function(req, res) {\n\n  console.log(&quot;New Access!!&quot;);\n  console.log(&quot;URL: &quot; + req.url);\n  console.log(&quot;Method: &quot; + req.method);\n\n  const url = `http:\/\/${req.headers.host}`;\n\n  \/\/ URL\u89e3\u6790\n  \/\/ req.url\u306f\u76f8\u5bfeURL\u304c\u5165\u308b\u3002\n  \/\/ url\u306f\u30d9\u30fc\u30b9url(\u76f8\u5bfeURL\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u57fa\u6e96\u3068\u306a\u308bURL)\u304c\u5165\u308b\u3002\n  const myURL = new URL(req.url, url);\n  const whitespace = 2;\n  const result = [];\n  const names = [];\n\n  let buf = &#039; &#039;;\n  let blanks = &#039;&#039;;\n\n  console.log(&quot;&quot;);\n\n  result.push(myURL.href);\n  result.push(myURL.origin);\n  result.push(myURL.protocol);\n  result.push(myURL.username);\n  result.push(myURL.password);\n  result.push(myURL.host);\n  result.push(myURL.port);\n  result.push(myURL.hostname);\n  result.push(myURL.pathname);\n  result.push(myURL.hash);\n  result.push(myURL.search);\n\n  names.push(&quot;href&quot;);\n  names.push(&quot;origin&quot;);\n  names.push(&quot;protocol&quot;);\n  names.push(&quot;username&quot;);\n  names.push(&quot;password&quot;);\n  names.push(&quot;host&quot;);\n  names.push(&quot;port&quot;);\n  names.push(&quot;hostname&quot;);\n  names.push(&quot;pathname&quot;);\n  names.push(&quot;hash&quot;);\n  names.push(&quot;search&quot;);\n\n  \/\/ names\u914d\u5217\u306e\u4e2d\u3067\u3001\u6700\u9577\u306e\u6587\u5b57\u5217\u3092longestString\u306b\u683c\u7d0d\u3059\u308b\u3002\n  const longestString = names.reduce((prev, current) =&gt; {\n    return prev.length &lt; current.length ? current : prev;\n  }, &#039;&#039;);\n\n  const stringLengthMax = longestString.length;\n\n  \/\/ buf\u306b\u683c\u7d0d\u3057\u305f\u7a7a\u306e\u6587\u5b57\u5217\u3092\u300cstringLengthMax + whitespace\u300d\u56de\u6570\u5206\u306e\u30b3\u30d4\u30fc\u3092\u542b\u3080\u65b0\u3057\u3044\u6587\u5b57\u5217blanks\u3092\u4f5c\u308b\u3002\u3000\n  blanks = buf.repeat(stringLengthMax + whitespace);\n\n  \/\/ URL\u306e\u89e3\u6790\u7d50\u679c\u3092\u51fa\u3059\u3002\n  names.forEach((t, u) =&gt; (console.log(t + &#039;:&#039; + blanks.slice(t.length) + result[u])));\n  console.log(&quot;&quot;);\n  \n  switch (myURL.pathname) {\n    case &quot;\/&quot;:\n      fs.readFile(&quot;.\/index.html&quot;, &quot;utf-8&quot;, function(err, data) {\n        res.writeHead(200, {\n          &quot;Content-Type&quot;: &quot;text\/html;charset=utf-8&quot;\n        });\n        res.write(data);\n        console.log(&quot;myURL.pathname = \/&quot;);\n        res.end();\n      });\n      break;\n    case &quot;\/styles.css&quot;:\n      fs.readFile(&quot;.\/styles.css&quot;, &quot;utf-8&quot;, function(err, data) {\n        res.writeHead(200, {\n          &quot;Content-Type&quot;: &quot;text\/css;charset=utf-8&quot;\n        });\n        res.write(data);\n        console.log(&quot;myURL.pathname = \/styles.css&quot;);\n        res.end();\n      });\n      break;\n    case &quot;\/test.js&quot;:\n      fs.readFile(&quot;.\/test.js&quot;, &quot;utf-8&quot;, function(err, data) {\n        res.writeHead(200, {\n          &quot;Content-Type&quot;: &quot;application\/javascript;charset=utf-8&quot;\n        });\n        res.write(data);\n        console.log(&quot;myURL.pathname = \/test.js&quot;);\n        res.end();\n      });\n      break;\n    default:\n      res.writeHead(404, {\n        &quot;Content-Type&quot;: &quot;text\/plain;charset=utf-8&quot;\n      });\n      res.write(&quot;404 Not Found&quot;);\n      res.end();\n  }\n\n  console.log(&quot;&quot;);\n\n}).listen(8000);\n<\/code><\/pre>\n\n\n\n<p>styles.css<\/p>\n\n\n\n<pre><code>\nbody {\n\twidth: 100%;\n    background-color: #b00;\n    color:#fff;\n}\n<\/code><\/pre>\n\n\n\n<p>index.html<\/p>\n\n\n\n<pre><code>\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;ja&quot;&gt;\n  &lt;head&gt;\n   &lt;link type=&quot;text\/css&quot; href=&quot;.\/styles.css&quot; rel=&quot;stylesheet&quot;&gt;\n    &lt;title&gt;Hello&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n  &lt;h1&gt;Hello Node.js!2023&lt;\/h1&gt;\n  &lt;p&gt;This is index.html&lt;\/p&gt;\n   &lt;script src=&quot;.\/test.js&quot;&gt;&lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>test.js<\/p>\n\n\n\n<pre><code>\nconsole.log(&quot;js\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3060\u3002&quot;);\n<\/code><\/pre>\n\n\n\n<p>main.js\u3092\u5b9f\u884c\u3059\u308b\u3068\u6b21\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<pre><code>\nC:\\Users\\\u3007\u3007\u3007\\test0819&gt;node main.js\nNew Access!!\nURL: \/\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\nmyURL.pathname = \/test.js\nmyURL.pathname = \/styles.css\nNew Access!!\nURL: \/favicon.ico\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/favicon.ico\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/favicon.ico\nhash:\nsearch:\n\n^C\nC:\\Users\\\u3007\u3007\u3007\\test0819&gt;node main.js\nNew Access!!\nURL: \/\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:\n\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\n\nmyURL.pathname = \/styles.css\nmyURL.pathname = \/test.js\nNew Access!!\nURL: \/favicon.ico\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/favicon.ico\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/favicon.ico\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:\n\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\n\nmyURL.pathname = \/test.js\nmyURL.pathname = \/styles.css\nNew Access!!\nURL: \/favicon.ico\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/favicon.ico\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/favicon.ico\nhash:\nsearch:\n\n\n^C\nC:\\Users\\\u3007\u3007\u3007\\test0819&gt;node main.js\nNew Access!!\nURL: \/\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:\n\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\n\nmyURL.pathname = \/styles.css\nmyURL.pathname = \/test.js\nNew Access!!\nURL: \/\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:\n\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\n\nmyURL.pathname = \/test.js\nmyURL.pathname = \/styles.css\nNew Access!!\nURL: \/?a=843tujg5huj\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/?a=843tujg5huj\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/\nhash:\nsearch:    ?a=843tujg5huj\n\n\nmyURL.pathname = \/\nNew Access!!\nURL: \/styles.css\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/styles.css\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/styles.css\nhash:\nsearch:\n\n\nNew Access!!\nURL: \/test.js\nMethod: GET\n\nhref:      http:\/\/localhost:8000\/test.js\norigin:    http:\/\/localhost:8000\nprotocol:  http:\nusername:\npassword:\nhost:      localhost:8000\nport:      8000\nhostname:  localhost\npathname:  \/test.js\nhash:\nsearch:\n\n\nmyURL.pathname = \/styles.css\nmyURL.pathname = \/test.js\n<\/code><\/pre>\n\n\n\n<script type=\"text\/javascript\">\njQuery(function($){\n$(\".jp-video\").hide();\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>fs.readFile\u95a2\u6570\u306f,\u975e\u540c\u671f\u7684\u306bhtml,css,js\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3975","post","type-post","status-publish","format-standard","hentry","category-node-js"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/3975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3975"}],"version-history":[{"count":7,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/3975\/revisions"}],"predecessor-version":[{"id":4262,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/3975\/revisions\/4262"}],"wp:attachment":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}