{"id":18931,"date":"2017-01-20T08:00:02","date_gmt":"2017-01-19T23:00:02","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=18931"},"modified":"2017-01-19T08:20:39","modified_gmt":"2017-01-18T23:20:39","slug":"node-js%e3%81%a7cors%e5%af%be%e5%bf%9c%e7%b0%a1%e6%98%93%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e7%94%a8%e6%84%8f%e3%81%99%e3%82%8b%e3%81%ab%e3%81%af","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/18931.html","title":{"rendered":"Node.js\u3067CORS\u5bfe\u5fdc\u7c21\u6613\u30b5\u30fc\u30d0\u3092\u7528\u610f\u3059\u308b\u306b\u306f"},"content":{"rendered":"<p>\u300c<a href=\"\/\/www.sssg.org\/blogs\/hiro345\/archives\/18928.html\">Angular2\u3092\u4f7f\u3063\u3066Web\u30b5\u30fc\u30d0\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u305f | hiro345<\/a>\u300d\u3067\u306f\u3001Python3\u304c\u697d\u3060\u3063\u305f\u306e\u3067\u63a1\u7528\u3057\u307e\u3057\u305f\u304c\u3001Node.js\u3067\u3082Web\u30b5\u30fc\u30d0\u30fc\u306f\u7528\u610f\u3067\u304d\u307e\u3059\u3002npm\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u3042\u308bconnect\u3084serve-static\u3092\u4f7f\u3046\u3068\u826f\u3055\u305d\u3046\u3067\u3057\u305f\u304c\u3001package.json\u3092\u4f7f\u3063\u305f\u308a\u3059\u308b\u306e\u3092\u8aac\u660e\u3059\u308b\u306e\u3082\u9762\u5012\u306a\u306e\u3067\u3001\u7d20\u306eNode.js\u3067CORS\u5bfe\u5fdc\u7c21\u6613\u30b5\u30fc\u30d0\u3092\u5b9f\u88c5\u3059\u308b\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n<!--more--><br \/>\n\u30d8\u30c3\u30c0\u30fc\u3092\u8a2d\u5b9a\u3057\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3088\u308aJavaScript\u306e&#8221;.js&#8221;\u306a\u3069\u306b\u5bfe\u5fdc\u3092\u3057\u305f\u65b9\u304c\u3044\u3044\u306e\u3067\u3057\u3087\u3046\u304c\u3001\u3053\u3093\u306a\u611f\u3058\u3001\u3068\u3044\u3046\u306e\u304c\u308f\u304b\u308c\u3070\u3044\u3044\u306e\u3067\u3001\u3053\u3046\u3057\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: javascript; gutter: true\">\r\nvar http = require(&quot;http&quot;);\r\nvar fs = require(&#039;fs&#039;);\r\nvar path = require(&#039;path&#039;);\r\n\r\nvar server = http.createServer(function (req, res) {\r\n  console.log(&#039;request starting...&#039;);\r\n  res.setHeader(&#039;Access-Control-Allow-Origin&#039;, &#039;*&#039;)\r\n  res.setHeader(&#039;Access-Control-Request-Method&#039;, &#039;*&#039;)\r\n  res.setHeader(&#039;Access-Control-Allow-Methods&#039;, &#039;OPTIONS, GET&#039;)\r\n  res.setHeader(&#039;Access-Control-Allow-Headers&#039;, &#039;*&#039;)\r\n  var filePath = &#039;.&#039; + req.url;\r\n  if (filePath == &#039;.\/&#039;) {\r\n    filePath = &#039;.\/index.html&#039;;\r\n  }\r\n  var extname = path.extname(filePath);\r\n  var contentType = &#039;text\/html&#039;;\r\n  switch (extname) {\r\n    case &#039;.json&#039;:\r\n      contentType = &#039;application\/json&#039;;\r\n      break;\r\n    case &#039;.png&#039;:\r\n      contentType = &#039;image\/png&#039;;\r\n      break;\r\n  }\r\n  fs.readFile(filePath, function(error, content) {\r\n    if (error) {\r\n      if(error.code == &#039;ENOENT&#039;){\r\n        res.writeHead(404);\r\n        res.end(&#039;404 Not found&#039;, &#039;utf-8&#039;);\r\n      } else {\r\n        res.writeHead(500);\r\n        res.end(&#039;500 Internal Error: &#039;+error.code+&#039;\\n&#039;);\r\n        res.end();\r\n      }\r\n    } else {\r\n        res.writeHead(200, { &#039;Content-Type&#039;: contentType });\r\n        res.end(content, &#039;utf-8&#039;);\r\n    }\r\n  });\r\n});\r\nserver.listen(process.env.PORT || 8000, function () {\r\n  console.log(&#039;Server running ...&#039;);\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u300cAngular2\u3092\u4f7f\u3063\u3066Web\u30b5\u30fc\u30d0\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u307f\u305f | hiro345\u300d\u3067\u306f\u3001Python3\u304c\u697d\u3060\u3063\u305f\u306e\u3067\u63a1\u7528\u3057\u307e\u3057\u305f\u304c\u3001Node.js\u3067\u3082Web\u30b5\u30fc\u30d0\u30fc\u306f\u7528\u610f\u3067\u304d\u307e\u3059\u3002npm\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u3042\u308bconnect &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/18931.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[1328,365],"class_list":["post-18931","post","type-post","status-publish","format-standard","hentry","category-linux","tag-http","tag-node-js"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/18931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=18931"}],"version-history":[{"count":1,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/18931\/revisions"}],"predecessor-version":[{"id":18932,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/18931\/revisions\/18932"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=18931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=18931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=18931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}