{"id":2481,"date":"2023-05-26T10:55:57","date_gmt":"2023-05-26T01:55:57","guid":{"rendered":"http:\/\/attacktube.com\/?p=2481"},"modified":"2023-06-08T11:57:11","modified_gmt":"2023-06-08T02:57:11","slug":"%e3%80%90node-js%e3%80%91http%e3%82%af%e3%83%a9%e3%82%a4%e3%82%a2%e3%83%b3%e3%83%88get%e3%83%aa%e3%82%af%e3%82%a8%e3%82%b9%e3%83%88%e3%81%9d%e3%81%ae1","status":"publish","type":"post","link":"https:\/\/attacktube.com\/?p=2481","title":{"rendered":"\u3010Node.js\u3011http\u30af\u30e9\u30a4\u30a2\u30f3\u30c8(POST\u30ea\u30af\u30a8\u30b9\u30c8)\u305d\u306e1"},"content":{"rendered":"\n<p>http\u30af\u30e9\u30a4\u30a2\u30f3\u30c8(POST\u30ea\u30af\u30a8\u30b9\u30c8)\u3068\u306f\u3001Node.js\u3067\u30b5\u30fc\u30d0\u30fc\u306eURL\u306bPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u3002<br>\u30d6\u30e9\u30a6\u30b6\u3067\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5834\u5408\u306fPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u9001\u3063\u3066\u3044\u308b\u3002<br>\u3053\u306ePOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092Node.js\u3067\u884c\u3046\u3002<br><br>\u74b0\u5883<br>Windows10(64bit)<br>nvm(Node Version Manager)\u3092\u4f7f\u7528\u3059\u308b\u3002<br>nvm\u306fnode\u3068npm\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u5207\u308a\u66ff\u3048\u304c\u3067\u304d\u308b\u3002<br>node.js\u306f\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u304c\u983b\u7e41\u306b\u884c\u308f\u308c\u308b\u305f\u3081\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u30c4\u30fc\u30eb\u3092\u4f7f\u3046\u3068\u90fd\u5408\u304c\u826f\u3044\u3053\u3068\u304c\u591a\u3044\u3002<br>\u300c\u7279\u5b9a\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u5fc5\u8981\u306aNode.js\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u7c21\u5358\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u300d\u3084\u300c\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u6bce\u306bNode.js\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u5207\u308a\u66ff\u3048\u304c\u7c21\u5358\u306b\u306a\u308b\u3002\u300d\u7b49\u306e\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308b\u3002<br>nvm 1.1.10<br>node v18.15.0<br>npm 9.5.0<\/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>Node.js\u3067\u30b5\u30fc\u30d0\u30fc\u306eURL\u306bpost\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u30b3\u30fc\u30c9\u300cpost.js\u300d\u3092\u6b21\u306b\u793a\u3059\u3002<\/p>\n\n\n\n<pre><code>\n(function() {\n\n    \/\/ Node.js\u306ehttp\u30e2\u30b8\u30e5\u30fc\u30eb\u3092require\u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u3001\u5b9a\u6570http\u306b\u683c\u7d0d\u3059\u308b\u3002\n    const http = require(&quot;http&quot;);\n    const data = &quot;PostData&quot;;\n    const url = &quot;http:\/\/localhost:3000&quot;;\n    const options = {\n        method: &quot;POST&quot;,\n        headers: {\n            &#039;Content-Type&#039;: &#039;text\/plain&#039;,\n            &#039;Content-Length&#039;: Buffer.byteLength(data),\n            &#039;User-Agent&#039;: &#039;Manual submission via get method from Node.js&#039;,\n        }\n    };\n\n    \/\/\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3059\u308b\u3002\n    \/\/ http\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089request\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002\n    \/\/ request\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u5f15\u6570\u3068\u3057\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3059\u308b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u53d7\u3051\u53d6\u308b\u3002\n    \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u6307\u5b9a\u3059\u308b\u3002\n    \/\/ \u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(res)\u3092\u53d7\u3051\u53d6\u308a\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u51e6\u7406\u3092\u884c\u3046\u3002\n    const req = http.request(url, options, (res) =&gt; {\n\n        console.log(`statusCode: ${res.statusCode}`);\/\/ \u30b9\u30c6\u30fc\u30bf\u30b9\u30b3\u30fc\u30c9\u3092\u8868\u793a\u3059\u308b\u3002\n        console.log(`content-type: ${res.headers[&#039;content-type&#039;]}`); \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u30bf\u30a4\u30d7\u3092\u8868\u793a\u3059\u308b\u3002\n        \n        \/\/ res.pipe(process.stdout)\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u6a19\u6e96\u51fa\u529b\u30b9\u30c8\u30ea\u30fc\u30e0\u306b\u30d1\u30a4\u30d7\u63a5\u7d9a\u3059\u308b\u3002\n        \/\/ pipe\u30e1\u30bd\u30c3\u30c9\u306f\u3001Readable\u30b9\u30c8\u30ea\u30fc\u30e0\u304b\u3089\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u53d6\u308a\u3001\u305d\u306e\u30c7\u30fc\u30bf\u3092Writable\u30b9\u30c8\u30ea\u30fc\u30e0\u306b\u66f8\u304d\u8fbc\u3080\u3002\n        \/\/ process.stdout\u306fNode.js\u306e\u6a19\u6e96\u51fa\u529b\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u8868\u3057\u3066\u304a\u308a\u3001res.pipe(process.stdout) \u306b\u3088\u3063\u3066\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c7\u30fc\u30bf\u304c\u8aad\u307f\u53d6\u3089\u308c\u3066\u6a19\u6e96\u51fa\u529b\u306b\u8868\u793a\u3055\u308c\u308b\u3002\n        \/\/ \u3064\u307e\u308a\u3001HTTP POST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\u3057\u305f\u5f8c\u3001\u305d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c7\u30fc\u30bf\u3092\u6a19\u6e96\u51fa\u529b\u306b\u8868\u793a\u3059\u308b\u3068\u3044\u3046\u52d5\u4f5c\u306b\u306a\u308b\u3002\n        \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(res)\u306f\u3001http.IncomingMessage\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u3042\u308b\u3002\n\n        res.pipe(process.stdout);\n\n        let responseData = &#039;&#039;;\n\n        \/\/ data\u53d7\u4fe1\u30a4\u30d9\u30f3\u30c8\u306e\u767a\u751f\u6642\u306b\u65ad\u7247\u30c7\u30fc\u30bf(chunk)\u3092\u53d6\u5f97\u3059\u308b\u3002\n        res.on(&#039;data&#039;, (chunk) =&gt; {\n           responseData += chunk;\n        });\n\n        \/\/ \u53d7\u4fe1\u5b8c\u4e86(end)\u30a4\u30d9\u30f3\u30c8\u767a\u751f\u6642\n        \/\/ \u300cend\u300d\u30a4\u30d9\u30f3\u30c8\u306f\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u53d6\u308a\u304c\u5b8c\u4e86\u3057\u305f\u5834\u5408\u306b\u767a\u751f\u3059\u308b\u3002\n        res.on(&#039;end&#039;, function() {\n            console.log(responseData);\n            console.log(&quot;\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u8aad\u307f\u53d6\u308a\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f&quot;);\n            \/\/ \u3053\u3053\u3067\u5fc5\u8981\u306a\u5f8c\u51e6\u7406\u3092\u5b9f\u884c\n        });\n\n    });\n\n    \/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u306e\u9001\u4fe1\u4e2d\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u300cerror\u300d\u30a4\u30d9\u30f3\u30c8\u306f\u767a\u751f\u3059\u308b\u3002\n    req.on(&#039;error&#039;, function(err) {\n        console.error(err.message);\n    });\n\n    \/\/ \u300cend\u300d\u30e1\u30bd\u30c3\u30c9\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u9001\u4fe1\u3092\u5b8c\u4e86\u3055\u305b\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3042\u308b\u3002\n    \/\/ \u300cend\u300d\u30e1\u30bd\u30c3\u30c9\u306f\u5b9f\u969b\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u9001\u4fe1\u3059\u308b\u3002\n    req.end(data);\n\n})();\n<\/code><\/pre>\n\n\n\n<p>post.js\u3092\u5b9f\u884c\u3059\u308b\u3002Node.js\u3067\u7acb\u3061\u4e0a\u3052\u305f\u30b5\u30fc\u30d0\u30fc\u300chttp:\/\/localhost:3000\u300d\u306b\u300cPOST\u30ea\u30af\u30a8\u30b9\u30c8\u300d\u3092\u9001\u4fe1\u3059\u308b\u3002<\/p>\n\n\n\n<pre><code>\nc:\\node\\server&gt;node post.js\nstatusCode: 200\ncontent-type: text\/plain;charset=utf-8\nmethod:POST\nreq.url:\/\nPostData\nHello World! POST\nmethod:POST\nreq.url:\/\nPostData\nHello World! POST\n\n\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u8aad\u307f\u53d6\u308a\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\n\nc:\\node\\server&gt;\n<\/code><\/pre>\n\n\n\n<p>\u30b5\u30fc\u30d0\u30fc\u304c\u7acb\u3061\u4e0a\u304c\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<pre><code>\nc:\\node\\server&gt;node post.js\nconnect ECONNREFUSED ::1:3000\n\nc:\\node\\server&gt;\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>http\u30af\u30e9\u30a4\u30a2\u30f3\u30c8(POST\u30ea\u30af\u30a8\u30b9\u30c8)\u3068\u306f\u3001Node.js\u3067\u30b5\u30fc\u30d0\u30fc\u306eURL\u306bPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u3002\u30d6\u30e9\u30a6\u30b6\u3067\u30b5\u30fc\u30d0\u30fc\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5834\u5408\u306fPOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u9001\u3063\u3066\u3044\u308b\u3002\u3053\u306ePOST\u30ea\u30af\u30a8\u30b9\u30c8\u3092Node [&hellip;]<\/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-2481","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\/2481","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=2481"}],"version-history":[{"count":14,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/2481\/revisions"}],"predecessor-version":[{"id":2861,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/2481\/revisions\/2861"}],"wp:attachment":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}