{"id":3101,"date":"2022-03-25T08:36:03","date_gmt":"2022-03-25T00:36:03","guid":{"rendered":"http:\/\/blog.odjbinail.cn\/?p=3101"},"modified":"2023-08-12T17:22:46","modified_gmt":"2023-08-12T09:22:46","slug":"%e5%85%a8%e6%a0%88%e5%bf%85%e5%a4%87%e5%9f%ba%e7%a1%80node-js","status":"publish","type":"post","link":"https:\/\/blog.odjbinail.cn\/?p=3101","title":{"rendered":"\u5168\u6808\u5fc5\u5907\u57fa\u7840Node.js"},"content":{"rendered":"<h1>Node.js \u57fa\u7840\u5165\u95e8<\/h1>\n<h2>nodejs \u662f\u4ec0\u4e48<\/h2>\n<ul>\n<li>\n<p>\u4e00\u4e2a\u57fa\u4e8e Chrome V8 \u5f15\u64ce\u7684 Javascript \u8fd0\u884c\u65f6<\/p>\n<ul>\n<li>2009 \u5e74\u53d1\u5e03,\u73b0\u5df2\u66f4\u65b0\u5230 V12 \u7248\u672c<\/li>\n<li>\u73b0\u5df2\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5f00\u6e90\u793e\u533a\u548c\u5404\u79cd\u516c\u53f8,\u7279\u522b\u662f\u4e92\u8054\u7f51\u516c\u53f8<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Chrome V8<\/p>\n<ul>\n<li>chrome \u662f\u4e00\u4e2a\u6d4f\u89c8\u5668,\u5b83\u53ef\u4ee5\u6267\u884c js \u4ee3\u7801<\/li>\n<li>V8 \u5c31\u662f chrome \u7684 js \u5f15\u64ce,\u4ee5\u901f\u5ea6\u8457\u79f0<\/li>\n<li>nodejs \u4e5f\u662f\u57fa\u4e8e js \u8bed\u6cd5\u7684,\u56e0\u6b64\u4e5f\u53ef\u4ee5\u501f\u7528 V8 \u5f15\u64ce<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>node \u51fa\u73b0\u4e4b\u524d<\/p>\n<ul>\n<li>nodejs \u51fa\u73b0\u4ee5\u524d,\u53ea\u6709\u6d4f\u89c8\u5668\u53ef\u4ee5\u6267\u884c js \u4ee3\u7801<\/li>\n<li>\u6d4f\u89c8\u5668\u4e3b\u8981\u662f\u663e\u793a\u7f51\u9875,\u6240\u6709 js \u4e5f\u88ab\u5f53\u505a\u7f51\u9875\u7684\u4e00\u90e8\u5206<\/li>\n<li>\u9664\u6b64\u4e4b\u5916,\u6ca1\u6709\u5176\u4ed6\u5e94\u7528\u573a\u666f,\u66f4\u522b\u63d0\u505a\u670d\u52a1\u7aef<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>node \u51fa\u73b0\u4e4b\u540e<\/p>\n<ul>\n<li>\u9664\u4e86\u6d4f\u89c8\u5668,nodejs \u53c8\u662f\u4e00\u4e2a\u65b0\u7684 js \u8fd0\u884c\u65f6<\/li>\n<li>\u54ea\u91cc\u5b89\u88c5 nodejs,\u54ea\u91cc\u5c31\u53ef\u4ee5\u8fd0\u884c js \u4ee3\u7801<\/li>\n<li>\u53ef\u4ee5\u7528\u5728\u672c\u673a(\u5982\u4f7f\u7528 webpack \u6253\u5305),\u4e5f\u53ef\u4ee5\u505a\u670d\u52a1\u7aef<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>nodejs \u7684\u4ef7\u503c<\/p>\n<ul>\n<li>\u8ba9 js&quot;\u653e\u98de\u81ea\u6211&quot;,\u4e0d\u518d\u662f\u7f51\u9875\u7684\u4e00\u90e8\u5206<\/li>\n<li>\u8ba9 js \u53ef\u4ee5\u505a\u66f4\u591a\u7684\u4e8b\u60c5<\/li>\n<li>\u4e5f\u8ba9\u524d\u7aef\u5de5\u7a0b\u5e08\u53ef\u4ee5\u505a\u66f4\u591a\u7684\u4e8b\u60c5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>nodejs \u521d\u4f53\u9a8c<\/h2>\n<ul>\n<li>cmd  node index.js<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst server = http.createServer((req, res) =&gt; {\n  const url = req.url \/\/ &#039;\/index.html?a=100&#039;\n  const path = url.split(&#039;?&#039;)[0] \/\/&#039;\/index.html&#039;\n  res.end(path+&#039;-123&#039;)\n})\nserver.listen(3000)<\/code><\/pre>\n<h2>npm \u4ecb\u7ecd<\/h2>\n<ul>\n<li>\n<p>node package manager,\u5373 nodejs \u8f6f\u4ef6\u5305\u7ba1\u7406\u8005<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/www.npmjs.com\/\">https:\/\/www.npmjs.com\/<\/a><\/p>\n<\/li>\n<li>\n<p>\u8f6f\u4ef6\u5305\u6709\u4f55\u7528<\/p>\n<ul>\n<li>\u73b0\u4ee3\u8f6f\u4ef6\u5de5\u7a0b\u5df2\u7ecf\u5b8c\u5584\u4e14\u6210\u719f,\u9879\u76ee\u4e0d\u4f1a\u4ece 0 \u5f00\u59cb\u5199<\/li>\n<li>\u5fc5\u987b\u642d\u914d\u6210\u719f\u7684\u5de5\u5177\u548c\u6846\u67b6\u624d\u80fd\u6ee1\u8db3\u9700\u6c42,\u5426\u5219\u5c06\u65e0\u4eba\u4f7f\u7528<\/li>\n<li>\u6bcf\u4e2a\u6210\u719f\u7684\u5f00\u53d1\u8bed\u8a00\u6216\u8005\u73af\u5883,\u90fd\u9700\u8981\u6210\u719f\u7684\u8f6f\u4ef6\u5305\u4f53\u7cfb<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5f00\u59cb\u4f7f\u7528 npm<\/p>\n<ul>\n<li>npm \u4f1a\u968f\u7740 nodejs \u4e00\u8d77\u88ab\u5b89\u88c5<\/li>\n<li>npm init \u521d\u59cb\u5316\u73af\u5883<\/li>\n<li>npm install lodash --save \u5b89\u88c5 lodash<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>npm \u5b89\u88c5 lodash<\/h2>\n<ul>\n<li>\n<p>npm init  \u4e00\u8def\u56de\u8f66  yes<\/p>\n<\/li>\n<li>\n<p>npm i lodash --save<\/p>\n<\/li>\n<li>\n<p>\u4fdd\u8bc1 lodash \u5728\u7a0b\u5e8f\u4e2d\u4f1a\u771f\u6b63\u7684\u5f15\u7528\u51fa\u6765,\u53bb\u4f7f\u7528<br \/>\n&quot;dependencies&quot;: {<br \/>\n&quot;lodash&quot;: &quot;^4.17.21&quot;<br \/>\n},<\/p>\n<\/li>\n<li>\n<p>lodash-test\/test.js<\/p>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const _ = require(&#039;lodash&#039;)\nconst arr = [1, 2, 3]\nconst otherArr = _.concat(arr, 4, 5)\nconsole.log(otherArr)<\/code><\/pre>\n<h2>npm \u5b89\u88c5 nodemon<\/h2>\n<ul>\n<li>npm install nodemon --save-dev<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code> &quot;scripts&quot;: {\n    &quot;test&quot;: &quot;echo \\&quot;Error: no test specified\\&quot; &amp;&amp; exit 1&quot;,\n    &quot;dev&quot;:&quot;nodemon index.js&quot;\n  },\n&quot;devDependencies&quot;: {\n    &quot;nodemon&quot;: &quot;^2.0.15&quot;\n  }<\/code><\/pre>\n<h2>comonjs-\u56de\u987e ES6 \u6a21\u5757\u5316<\/h2>\n<ul>\n<li>commonjs \u8bed\u6cd5\u4ecb\u7ecd\n<ul>\n<li>commonjs \u548c ES6 Module \u7684\u533a\u522b<\/li>\n<li>\u4e3a\u4f55\u8981\u4f7f\u7528\u6a21\u5757\u5316<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>commonjs \u8bed\u6cd5\u4ecb\u7ecd<\/h2>\n<ul>\n<li>\n<p>module.exports<\/p>\n<\/li>\n<li>\n<p>require(...)<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u8981\u7528\u4e8e nodejs \u5f00\u53d1<\/p>\n<\/li>\n<li>\n<p>require(...)\u7684\u4e09\u4e2a\u5c42\u7ea7<\/p>\n<ul>\n<li>\u7cfb\u7edf\u81ea\u5e26\u6a21\u5757,\u5982 require('http')<\/li>\n<li>npm \u5305,\u5982 require('lodash')<\/li>\n<li>\u81ea\u5b9a\u4e49\u6a21\u5757,\u5982 require('a')<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>commonjs-test-&gt;utils.js<\/p>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>function sum (a, b) {\n  return a + b\n}\nfunction test () {\n  console.log(&#039;this is test&#039;)\n}\nmodule.exports = { sum, test }<\/code><\/pre>\n<ul>\n<li>commonjs-test-&gt;index.js<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const { sum, test } = require(&#039;.\/utils&#039;)\ntest()\nconst res = sum(10, 30)\nconsole.log(res)<\/code><\/pre>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\/\/nodejs \u81ea\u5e26\u7684\u6a21\u5757\nconst _ = require(&#039;lodash&#039;)\/\/\u901a\u8fc7 npm \u5b89\u88c5\u7684\u6a21\u5757\nconst { sum, test } = require(&#039;.\/utils&#039;) \/\/\u81ea\u5df1\u624b\u5199\u7684\u6a21\u5757 <\/code><\/pre>\n<h2>commonjs \u548c ES6 Module \u7684\u533a\u522b<\/h2>\n<ul>\n<li>\u4e24\u8005\u8bed\u6cd5\u4e0d\u4e00\u6837<\/li>\n<li>commonjs \u662f\u6267\u884c\u65f6\u5f15\u5165,\u52a8\u6001\u7684<\/li>\n<li>ES6 Module \u662f\u6253\u5305(\u63d0\u524d)\u65f6\u5f15\u5165,\u9759\u6001\u7684<\/li>\n<\/ul>\n<h2>\u4e3a\u4f55\u4f7f\u7528\u6a21\u5757\u5316<\/h2>\n<ul>\n<li>\u6a21\u5757\u62c6\u5206\u5f00,\u4fbf\u4e8e\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u7ba1\u7406<\/li>\n<li>\u4fbf\u4e8e\u591a\u4eba\u534f\u4f5c\u5f00\u53d1,\u5404\u5199\u5404\u7684\u4e92\u4e0d\u5e72\u6270<\/li>\n<li>\u6210\u719f\u7684\u8bed\u8a00,\u90fd\u652f\u6301\u6a21\u5757\u5316,\u5982 C, C++, Java, php, Python, Go<\/li>\n<\/ul>\n<h2>debug \u4ecb\u7ecd\u548c\u6f14\u793a<\/h2>\n<ul>\n<li>bug \u5373\u9519\u8bef\n<ul>\n<li>debug \u5373\u6392\u9519,\u4e5f\u53eb\u8c03\u8bd5<\/li>\n<li>\u7f16\u7a0b\u8bed\u8a00,\u5fc5\u987b\u6709\u6210\u719f\u7684 debug \u673a\u5236,\u5426\u5219\u5c06\u4e0d\u53ef\u7528<\/li>\n<\/ul>\n<\/li>\n<li>debug \u7684\u91cd\u8981\u6027\n<ul>\n<li>\u7a0b\u5e8f\u51fa bug \u5f88\u5e38\u89c1,\u56e0\u6b64 debug \u4e5f\u5f88\u5e38\u7528<\/li>\n<li>\u51fa\u4e86 bug,\u5f97\u9700\u8981\u77e5\u9053\u4ee3\u7801\u662f\u5982\u4f55\u8fd0\u884c\u51fa\u9519\u7684<\/li>\n<li>\u5c31\u50cf\u4e00\u53f0\u673a\u5668\u5f02\u54cd,\u5f97\u62c6\u5f00\u5916\u58f3\u770b\u770b\u91cc\u9762\u5982\u4f55\u8fd0\u8f6c\u7684<\/li>\n<\/ul>\n<\/li>\n<li>inspect \u8c03\u8bd5\u6cd5\n<ul>\n<li>\u4fee\u6539 scrtips,\u589e\u52a0--inspect,\u542f\u52a8\u670d\u52a1<\/li>\n<li>\u6253\u5f00 chrome,\u8bbf\u95ee chrome:\/\/inspect<\/li>\n<li>\u589e\u52a0 debugger,\u91cd\u542f\u670d\u52a1,\u5373\u53ef\u8c03\u8bd5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\n\/\/ const a = undefined\nconst server = http.createServer((req, res) =&gt; {\n  \/\/ debugger\/\/\u65ad\u70b9\n  const url = req.url \/\/ &#039;\/index.html?a=100&#039;\n  const path = url.split(&#039;?&#039;)[0] \/\/&#039;\/index.html&#039;\n  \/\/ a()\n  res.end(path + &#039;1234&#039;)\n})\nserver.listen(3000)\nconsole.log(&#039;server listening on 3000 port&#039;)<\/code><\/pre>\n<h2>nodejs \u548c\u524d\u7aef js \u7684\u533a\u522b<\/h2>\n<ul>\n<li>\n<p>\u4e24\u8005\u90fd\u4f7f\u7528 js \u8bed\u6cd5<\/p>\n<ul>\n<li>\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u7c7b\u578b<\/li>\n<li>\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u6267\u884c<\/li>\n<li>ES6 \u7684 Class Promise \u7b49\u8bed\u6cd5<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u524d\u7aef js \u4f7f\u7528\u6d4f\u89c8\u5668\u63d0\u4f9b\u7684 Web API<\/p>\n<ul>\n<li>\u5982\u524d\u7aef\u7f51\u9875\u7684 DOM BOM \u4e8b\u4ef6 Ajax \u7b49<\/li>\n<li>\u524d\u7aef JS \u53ef\u4ee5\u4f7f\u7528,\u56e0\u4e3a\u5728\u6d4f\u89c8\u5668\u73af\u5883<\/li>\n<li>nodejs \u5219\u65e0\u6cd5\u4f7f\u7528,\u56e0\u4e3a\u662f nodejs \u73af\u5883<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>nodejs \u4f7f\u7528 nodejs API<\/p>\n<ul>\n<li>\u5982\u5904\u7406 http<\/li>\n<li>nodejs \u53ef\u4ee5\u4f7f\u7528,\u56e0\u4e3a\u662f nodejs \u73af\u5883<\/li>\n<li>\u524d\u7aef JS \u5219\u65e0\u6cd5\u4f7f\u7528,.\u56e0\u4e3a\u5728\u6d4f\u89c8\u5668\u73af\u5883<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u524d\u7aef js=js \u8bed\u6cd5+Web API<\/p>\n<\/li>\n<li>\n<p>nodejs=js \u8bed\u6cd5+nodejs API<\/p>\n<\/li>\n<li>\n<p>\u540c\u6837\u7684\u8bed\u6cd5,\u4e0d\u540c\u7684\u5de5\u4f5c<\/p>\n<\/li>\n<\/ul>\n<h1>\u524d\u540e\u7aef\u90a3\u4e9b\u4e8b\u513f<\/h1>\n<h2>\u4e3a\u4f55\u8981\u8bb2\u8fd9\u95e8\u8bfe<\/h2>\n<ul>\n<li>\n<p>\u524d\u7aef\u548c\u670d\u52a1\u7aef\u90a3\u4e9b\u4e8b\u513f<\/p>\n<ul>\n<li>nodejs \u5165\u95e8\u548c\u4f7f\u7528<\/li>\n<li>koa2 \u6846\u67b6<\/li>\n<li>\u6570\u636e\u5e93<\/li>\n<li>\u767b\u5f55\u548c\u7528\u6237\u6821\u9a8c<\/li>\n<li>\u7efc\u5408\u9879\u76ee\u5b9e\u6218<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f01\u4e1a\u8981\u6c42\u524d\u7aef\u5de5\u7a0b\u5e08,\u8981\u4f1a\u5199\u670d\u52a1\u7aef<\/p>\n<ul>\n<li>\u9762\u8bd5\u65f6,nodejs \u5df2\u6210\u4e3a\u7edd\u5bf9\u52a0\u5206\u9879<\/li>\n<li>\u524d\u7aef\u5de5\u7a0b\u5e08\u7684\u804c\u4e1a\u53d1\u5c55,\u79bb\u4e0d\u5f00\u670d\u52a1\u7aef<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f01\u4e1a\u9700\u6c42<\/p>\n<ul>\n<li>\u8282\u7701\u4eba\u529b\u6210\u672c<\/li>\n<li>\u8282\u7701\u6c9f\u901a\u6210\u672c<\/li>\n<li>\u8282\u7701\u5f00\u53d1\u65f6\u95f4<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u524d\u7aef\u5de5\u7a0b\u5e08\u7684\u804c\u4e1a\u53d1\u5c55,\u79bb\u4e0d\u5f00\u670d\u52a1\u7aef<\/p>\n<ul>\n<li>\u524d\u7aef\u4e0d\u80fd\u8131\u79bb\u670d\u52a1\u7aef\u72ec\u7acb\u5b58\u5728<\/li>\n<li>\u505a\u524d\u7aef\u5f00\u53d1,\u5fc5\u987b\u4e86\u89e3\u670d\u52a1\u7aef,\u4eb2\u5bc6\u534f\u4f5c<\/li>\n<li>\u505a\u524d\u7aef\u67b6\u6784\u5e08,\u5fc5\u987b\u719f\u6089,\u751a\u81f3\u5f15\u9886\u670d\u52a1\u7aef\u5f00\u53d1<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u8bfe\u7a0b\u5b89\u6392<\/h2>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032423251965.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032423251965.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u4ec0\u4e48\u662f\u670d\u52a1\u7aef<\/h2>\n<ul>\n<li>\n<p>\u670d\u52a1\u7aef,\u53c8\u79f0\u540e\u7aef,server \u7aef<\/p>\n<ul>\n<li>\u524d\u7aef\u662f\u7528\u6237\u53ef\u89c1, \u53ef\u64cd\u4f5c\u7684\u90e8\u5206,\u5982\u6811\u679d\u6811\u53f6<\/li>\n<li>\u670d\u52a1\u7aef\u4e3a\u524d\u7aef\u63d0\u4f9b&quot;\u652f\u6491&quot;\u548c&quot;\u8425\u517b&quot;,\u5982\u6811\u6839<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u670d\u52a1\u7aef\u7684\u804c\u8d23<\/p>\n<ul>\n<li>\u63d0\u4f9b\u524d\u7aef\u8981\u5c55\u793a\u7684\u6570\u636e<\/li>\n<li>\u63a5\u6536\u524d\u7aef\u8981\u63d0\u4ea4\u7684\u6570\u636e<\/li>\n<li>\u5b58\u50a8\u6570\u636e ( \u8f6f\u4ef6\u516c\u53f8\u975e\u5e38\u770b\u91cd\u6570\u636e,\u4f1a\u6536\u96c6\u5404\u79cd\u6570\u636e )<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u670d\u52a1\u7aef\u7684\u8868\u73b0\u5f62\u5f0f<\/p>\n<ul>\n<li>\u524d\u7aef ajax \u8981\u8c03\u7528\u67d0\u4e2a\u63a5\u53e3<\/li>\n<li>\u8fd9\u4e2a\u63a5\u53e3,\u5c31\u662f\u670d\u52a1\u7aef\u63d0\u4f9b\u7684<\/li>\n<li>\u5982 get \u8bf7\u6c42\u83b7\u53d6\u6570\u636e,post \u8bf7\u6c42\u63d0\u4ea4\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u524d\u7aef<\/p>\n<ul>\n<li>\u72ed\u4e49: \u7f51\u9875<\/li>\n<li>\u5e7f\u4e49: \u5404\u79cd\u5ba2\u6237\u7aef,\u5982 App, PC \u5ba2\u6237\u7aef\u7b49<\/li>\n<li>\u672c\u8bfe\u7a0b\u4e2d,\u524d\u7aef\u5373\u7f51\u9875<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u7559\u8a00\u677f\u6848\u4f8b<\/h2>\n<ul>\n<li>\u5f00\u53d1\u524d\u7aef\u9875\u9762,\u5982\u7528 Vue<\/li>\n<li>\u9700\u8981 Ajax \u7684\u63a5\u53e3: \u83b7\u53d6\u7559\u8a00\u5217\u8868,\u63d0\u4ea4\u7559\u8a00<\/li>\n<li>\u5f00\u53d1\u670d\u52a1\u7aef,\u63d0\u4f9b\u63a5\u53e3<\/li>\n<\/ul>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032512012229.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032512012229.png\" alt=\"\" \/><\/p><\/noscript>\n<ul>\n<li>\u5c0f\u7ed3\n<ul>\n<li>\u670d\u52a1\u7aef(\u540e\u7aef\/server)\u4e3a\u524d\u7aef\u63d0\u4f9b\u652f\u6491\u548c\u8425\u517b,\u50cf\u4e00\u68f5\u6811<\/li>\n<li>\u670d\u52a1\u7aef\u7684\u804c\u8d23: \u63d0\u4f9b\u6570\u636e,\u63a5\u6536\u6570\u636e,\u5b58\u50a8\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u524d\u540e\u7aef\u5982\u4f55\u4ea4\u4e92-http \u534f\u8bae<\/h2>\n<ul>\n<li>\n<p>\u524d\u7aef\u9875\u9762\u4e0d\u4e00\u5b9a\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u7aef<\/p>\n<\/li>\n<li>\n<p>http \u534f\u8bae-\u4ecb\u7ecd<\/p>\n<ul>\n<li>\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8bae HyperText Transfer Protocol<\/li>\n<li>\u89c4\u5b9a\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5982\u4f55\u901a\u8baf<\/li>\n<li>\u662f\u4e92\u8054\u7f51\u4e16\u754c\u6570\u636e\u901a\u8baf\u7684\u6807\u51c6\u548c\u57fa\u77f3<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>http \u534f\u8bae-\u901a\u7528<\/p>\n<ul>\n<li>url : \u540e\u7aef\u63a5\u53e3\u7684\u5730\u5740,\u5373\u524d\u7aef Ajax \u8bf7\u6c42\u65f6\u7684\u5730\u5740<\/li>\n<li>method : \u8bf7\u6c42\u65b9\u6cd5,\u5982 GET POST PUT DELETE \u7b49<\/li>\n<li>\u72b6\u6001\u7801 : \u63a5\u53e3\u8fd4\u56de\u7684\u72b6\u6001, \u5982 200(\u6210\u529f) 302(\u91cd\u5b9a\u5411) 404(\u6ca1\u627e\u5230) 500(\u53d1\u751f\u9519\u8bef)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>http \u534f\u8bae - Request<\/p>\n<ul>\n<li>Request : \u8bf7\u6c42<\/li>\n<li>Request Body : \u8bf7\u6c42\u662f\u53d1\u9001\u7ed9\u540e\u7aef\u7684\u6570\u636e<\/li>\n<li>Request Content-type : \u53d1\u9001\u6570\u636e\u7684\u683c\u5f0f , \u5982 json<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>http \u534f\u8bae - Response<\/p>\n<ul>\n<li>Response : \u8fd4\u56de\/\u54cd\u5e94<\/li>\n<li>Response Body : \u540e\u7aef\u8fd4\u56de\u7ed9\u524d\u7aef\u7684\u6570\u636e<\/li>\n<li>Response Content-type : \u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f,,\u5982 json<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032516575616.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032516575616.png\" alt=\"\" \/><\/p><\/noscript>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032516583146.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032516583146.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u524d\u540e\u7aef\u5982\u4f55\u4ea4\u4e92-\u7559\u8a00\u677f\u793a\u4f8b<\/h2>\n<ul>\n<li>errno : 0 ( \u5373 error number \u7b80\u5199 )<\/li>\n<li>data : { ... }<\/li>\n<li>message : &quot;...&quot;<\/li>\n<\/ul>\n<h2>\u524d\u540e\u7aef\u5982\u4f55\u4ea4\u4e92\u4e00\u4e2a\u9875\u9762\u5bf9\u5e94\u591a\u4e2a\u670d\u52a1<\/h2>\n<ul>\n<li>\u4e0d\u540c\u8d44\u6e90\u53ef\u80fd\u6765\u81ea\u4e0d\u540c\u7684\u57df\u540d\n<ul>\n<li>html \u53ef\u80fd\u6765\u81ea\u4e00\u4e2a\u5355\u72ec\u7684\u57df\u540d<\/li>\n<li>js css \u53ef\u80fd\u6765\u81ea\u4e00\u4e2a\u72ec\u7acb\u7684\u57df\u540d<\/li>\n<li>\u6570\u636e\u53ef\u80fd\u6765\u81ea\u4e00\u4e2a\u72ec\u7acb\u7684\u57df\u540d<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032517234919.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032517234919.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u670d\u52a1\u7aef\u5982\u4f55\u5904\u7406\u5e76\u8fd4\u56de\u6570\u636e<\/h2>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u524d\u7aef\u8bf7\u6c42\u7684 url \u89c4\u5219--\u8def\u7531<\/p>\n<\/li>\n<li>\n<p>\u7528 Request \u83b7\u53d6\u6570\u636e,\u7528 Response \u8fd4\u56de\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u8bfb\u53d6\u548c\u5b58\u50a8\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u8def\u7531\u662f\u4ec0\u4e48<\/p>\n<ul>\n<li>router<\/li>\n<li>\u670d\u52a1\u7aef\u7684\u5165\u53e3\u89c4\u5219<\/li>\n<li>\u548c\u524d\u7aef\u7684\u7ea6\u5b9a<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8def\u7531\u5305\u542b\u4ec0\u4e48<\/p>\n<ul>\n<li>\u5b9a\u4e49 method,\u5982 GET\/POST \u7b49<\/li>\n<li>\u5b9a\u4e49 url \u89c4\u5219,\u5982\/api\/list \u548c\/api\/create<\/li>\n<li>\u5b9a\u4e49\u8f93\u5165(Request body) \u548c\u8f93\u51fa (Response body) \u683c\u5f0f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8def\u7531\u548c url<\/p>\n<ul>\n<li>GET \/api\/list \u8def\u7531 -&gt; axios.get( '\/api\/list?a=1')<\/li>\n<li>POST \/api\/create \u8def\u7531 -&gt;axios.get('\/api\/create',{...})<\/li>\n<li>\u8def\u7531\u662f\u89c4\u5219,url \u662f\u5177\u4f53\u7684\u5f62\u5f0f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8def\u7531-\u5c0f\u7ed3<\/p>\n<ul>\n<li>\u89c4\u5b9a\u670d\u52a1\u7aef\u7684\u5165\u53e3,\u5165\u53e3\u5373\u529f\u80fd<\/li>\n<li>\u5b9a\u4e49 method,url \u89c4\u5219,\u8f93\u5165\u8f93\u51fa\u683c\u5f0f<\/li>\n<li>\u8def\u7531\u662f\u89c4\u5219,url \u662f\u5177\u4f53\u7684\u5f62\u5f0f,\u4e24\u8005\u4e0d\u540c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u670d\u52a1\u7aef\u5982\u4f55\u5904\u7406\u6570\u636e- req \u548c res<\/h2>\n<ul>\n<li>\n<p>\u670d\u52a1\u7aef\u80fd\u5f97\u5230 Request \u548c Response<\/p>\n<ul>\n<li>\u4e5f\u53ef\u7b80\u79f0\u4e3a req \u548c res<\/li>\n<li>\u901a\u8fc7 Request \u53ef\u83b7\u53d6: method url body<\/li>\n<li>\u901a\u8fc7 Response : \u53ef\u8bbe\u7f6e: \u72b6\u6001\u7801,Content-type,body<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8def\u7531\u89c4\u5219\u7684\u5b9e\u73b0\u79bb\u4e0d\u5f00 Request<\/p>\n<ul>\n<li>\u5148\u5b9a\u4e49\u8def\u7531\u89c4\u5219(method,url)<\/li>\n<li>\u901a\u8fc7 Request \u53ef\u83b7\u53d6: method url<\/li>\n<li>\u68c0\u67e5 method \u548c url \u662f\u5426\u7b26\u5408\u54ea\u4e2a\u8def\u7531<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u670d\u52a1\u7aef\u5982\u4f55\u5904\u7406\u6570\u636e- \u6570\u636e\u5e93<\/h2>\n<ul>\n<li>\u6570\u636e\u5e93,\u4e13\u804c\u505a\u6570\u636e\u7684\u5b58\u50a8\u548c\u67e5\u8be2<\/li>\n<li>\u6570\u636e\u5e93\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u7cfb\u7edf,\u4e0d\u662f nodejs \u4e13\u5c5e\u7684<\/li>\n<li>\u5148\u4e0d\u7ba1\u6570\u636e\u5e93\u662f\u4ec0\u4e48,\u5c31\u5148\u4ee5\u4e3a\u662f\u4e00\u4e2a JSON<\/li>\n<\/ul>\n<p><img data-original=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032618442293.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"http:\/\/djblog.odjbinail.cn\/wp-content\/uploads\/2022\/03\/2022032618442293.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u4e3a\u4f55\u9009\u62e9 nodejs \u505a\u670d\u52a1\u7aef<\/h2>\n<ul>\n<li>\u4e0d\u540c\u8bed\u8a00,\u90fd\u80fd\u5b9e\u73b0\u670d\u52a1\u7aef\u804c\u8d23\n<ul>\n<li>\u5904\u7406 http \u8bf7\u6c42<\/li>\n<li>\u5b9a\u4e49\u8def\u7531,\u4f9b\u524d\u7aef Ajax \u8bbf\u95ee<\/li>\n<li>\u4f7f\u7528\u6570\u636e\u5e93,\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<li>\u4e0d\u540c\u8bed\u8a00,\u6709\u54ea\u4e9b\u4e0d\u540c\n<ul>\n<li>\u8bed\u8a00\u8bed\u6cd5\u548c\u5185\u7f6e API<\/li>\n<li>\u6846\u67b6<\/li>\n<li>\u5de5\u5177\u5305\u6216\u8f6f\u4ef6\u5305(\u5982 npm)<\/li>\n<\/ul>\n<\/li>\n<li>\u521d\u5b66\u8005\u5b66\u4e60\u670d\u52a1\u7aef\u7684\u91cd\u70b9\n<ul>\n<li>\u638c\u63e1\u670d\u52a1\u7aef\u7684\u6d41\u7a0b\u548c\u5957\u8def<\/li>\n<li>\u80fd\u5feb\u901f\u505a\u51fa\u9879\u76ee,\u5728\u5b9e\u8df5\u4e2d\u8fdb\u6b65<\/li>\n<li>\u4e0d\u5e94\u8be5\u88ab\u8bed\u8a00,\u8bed\u6cd5\u7b49\u55a7\u5bbe\u593a\u4e3b<\/li>\n<\/ul>\n<\/li>\n<li>nodejs \u5bf9\u4e8e\u524d\u7aef\u5de5\u7a0b\u5e08\u7684\u4f18\u52bf\n<ul>\n<li>nodejs \u4f7f\u7528 JS \u8bed\u6cd5<\/li>\n<li>nodejs \u4f7f\u7528 npm<\/li>\n<li>\u8fd8\u9700\u8981\u5b66\u4e60\u6846\u67b6\u548c API<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u5b66\u4e60 nodejs-\u5229\u7528\u7f51\u7edc<\/h2>\n<ul>\n<li>\n<p>\u5b66\u4e60\u4e00\u95e8\u65b0\u6280\u672f\u7684\u6b63\u786e\u5957\u8def<\/p>\n<ul>\n<li>\u7b2c\u4e00 , \u627e\u51c6\u8303\u56f4<\/li>\n<li>\u7b2c\u4e8c , \u523b\u610f\u8bad\u7ec3<\/li>\n<li>\u7b2c\u4e09 , \u53ca\u65f6\u53cd\u9988<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5584\u7528\u6587\u6863<\/p>\n<ul>\n<li>\u7a0b\u5e8f\u5458\u6bcf\u5929\u90fd\u4f1a\u63a5\u89e6\u65b0\u4e1c\u897f<\/li>\n<li>\u5fc5\u987b\u5b66\u4f1a\u770b\u6587\u6863<\/li>\n<li>\u4f46\u6ce8\u610f,\u6587\u6863\u5e76\u4e0d\u9002\u5408\u4ece 0 \u5165\u95e8<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>Node.js \u5904\u7406 HTTP<\/h1>\n<h2>\u8bfe\u7a0b\u4ecb\u7ecd<\/h2>\n<ul>\n<li>\u5b9a\u4e49\u8def\u7531\n<ul>\n<li>\u62ff\u5230 req \u4e2d\u7684 url \u548c method<\/li>\n<li>\u5b9a\u4e49\u8def\u7531(\u6a21\u62df\u7559\u8a00\u677f\u7684\u83b7\u53d6\u7559\u8a00\u5217\u8868,\u521b\u5efa\u7559\u8a00)<\/li>\n<li>\u6d4b\u8bd5\u8def\u7531(\u9700\u8981\u5b89\u88c5 postman)<\/li>\n<li>querystring<\/li>\n<\/ul>\n<\/li>\n<li>\u83b7\u53d6\u5b8c\u6574 url\n<ul>\n<li>\u4ece url \u5206\u6790 query \u53c2\u6570<\/li>\n<li>\u76f4\u63a5\u4f7f\u7528 nodejs \u81ea\u5e26\u7684 querystring<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<p>\u8fd4\u56de\u6570\u636e<\/p>\n<ul>\n<li>\u4f7f\u7528 res \u8bbe\u7f6e\u8fd4\u56de\u7684\u72b6\u6001\u7801,Content-type,Body<\/li>\n<li>\u5982\u4f55\u8fd4\u56de JSON \u6570\u636e<\/li>\n<li>\u5982\u4f55\u8fd4\u56de html \u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u83b7\u53d6 Request Body<\/p>\n<ul>\n<li>\u6d41 stream \u7684\u6982\u5ff5<\/li>\n<li>\u6f14\u793a\u5982\u4f55\u83b7\u53d6 Request Body<\/li>\n<li>\u4f7f\u7528 postman \u6d4b\u8bd5<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u8ba4\u8bc6 req \u548c res-\u76d1\u542c http \u8bf7\u6c42<\/h2>\n<ul>\n<li>nodejs \u5982\u4f55\u76d1\u542c http \u8bf7\u6c42\n<ul>\n<li>\u5982\u4f55\u62ff\u5230 req \u548c res<\/li>\n<li>\u5982\u4f55\u4f7f\u7528 req \u548c res<\/li>\n<\/ul>\n<\/li>\n<li>nodejs \u542f\u52a8 Web \u670d\u52a1\n<ul>\n<li>\u4f7f\u7528 http \u6a21\u5757,\u542f\u52a8\u670d\u52a1<\/li>\n<li>\u672c\u673a\u7684 IP127.0.0.1<\/li>\n<li>\u672c\u673a\u7684\u57df\u540d localhost<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst server=http.createServer(()=&gt;{\n  console.log(&#039;\u5df2\u7ecf\u6536\u5230 http \u8bf7\u6c42&#039;)\n  \/\/\u8fd8\u6ca1\u6709\u8fd4\u56de\u4efb\u4f55\u4e1c\u897f\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)\n<\/code><\/pre>\n<h2>\u8ba4\u8bc6 req \u548c res-\u83b7\u53d6\u548c\u4f7f\u7528<\/h2>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst server=http.createServer((req,res)=&gt;{\n  \/\/console.log(&#039;\u5df2\u7ecf\u6536\u5230 http \u8bf7\u6c42&#039;)\n  const url=req.url \/\/http:\/\/localhost:3000\/index.html\n  console.log(&#039;url is: &#039;,url) \/\/ \/index.html\n  res.end(&#039;hello world&#039;)\/\/res \u8fd4\u56de\u4fe1\u606f\u7ed9\u524d\u7aef\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)\n<\/code><\/pre>\n<h2>\u8def\u7531-\u5b9a\u4e49\u4e00\u4e2a get \u8def\u7531<\/h2>\n<ul>\n<li>nodejs \u5b9a\u4e49\u8def\u7531\n<ul>\n<li>\u4ece req \u4e2d\u83b7\u53d6 url \u548c method<\/li>\n<li>\u5224\u65ad method \u662f\u5426\u7b26\u5408<\/li>\n<li>\u770b url \u662f\u5426\u7b26\u5408\u89c4\u5219<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst server = http.createServer((req, res) =&gt; {\n  const url = req.url\n  const path=url.split(&#039;?&#039;)[0]\n  const method = req.method\n  \/\/ console.log(&#039;url is: &#039;,url)\/\/  \/api\/list?a=200\n  \/\/ console.log(&#039;method is: &#039;,method)\n  \/\/\u5b9a\u4e49\u8def\u7531: \u6a21\u62df\u83b7\u53d6\u7559\u8a00\u677f\u5217\u8868\n  if (path=== &#039;\/api\/list&#039; &amp;&amp; method === &#039;GET&#039;) {\n    res.end(&#039;this is list router&#039;)\n  } else { res.end(&#039;404&#039;) }\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)\n<\/code><\/pre>\n<h2>\u8def\u7531-\u5b9a\u4e49\u4e00\u4e2a post \u8def\u7531<\/h2>\n<ul>\n<li>get \u8bf7\u6c42,\u76f4\u63a5\u62ff\u6d4f\u89c8\u5668\u8bbf\u95ee<\/li>\n<li>post \u8bf7\u6c42,\u9700\u8981\u501f\u52a9\u5de5\u5177--postman<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>\/\/\u5b9a\u4e49\u8def\u7531:\u6a21\u62df\u521b\u5efa\u7559\u8a00\n if(path===&#039;\/api\/create&#039; &amp;&amp; method===&#039;POST&#039;){\n    res.end(&#039;this is create router&#039;)\n  }<\/code><\/pre>\n<h2>querystring-\u4ecb\u7ecd\u548c\u4f7f\u7528<\/h2>\n<ul>\n<li>\n<p>\u4ec0\u4e48\u662f querystring<\/p>\n<\/li>\n<li>\n<p>nodejs \u83b7\u53d6 querystring<\/p>\n<\/li>\n<li>\n<p>\u8bba\u7ed3\u6784\u5316\u4e0e\u975e\u7ed3\u6784\u5316<\/p>\n<ul>\n<li>url \u95ee\u53f7?\u540e\u9762\u7684\u90fd\u662f querystring (\u4e5f\u53eb url \u53c2\u6570)<\/li>\n<li>&amp;\u5206\u5272,key=value \u5f62\u5f0f,\u53ef\u7ee7\u7eed\u6269\u5c55<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>querystring \u7684\u4f5c\u7528<\/p>\n<ul>\n<li>\u52a8\u6001\u7f51\u9875\u7684\u57fa\u77f3 (hash \u5c31\u4e0d\u884c)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5982\u4f55\u5229\u7528 querystring \u5b9e\u73b0\u52a8\u6001\u7f51\u9875<\/p>\n<ul>\n<li>\u670d\u52a1\u7aef\u62ff\u5230 querystring<\/li>\n<li>\u6839\u636e\u4e0d\u540c\u7684 querystring,\u8fd4\u56de\u4e0d\u540c\u5185\u5bb9<\/li>\n<li>\u5373\u53d8\u5316 querystring,\u5c31\u662f\u53d8\u6362\u5185\u5bb9(\u53ea\u8981\u670d\u52a1\u7aef\u652f\u6301)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst server = http.createServer((req, res) =&gt; {\n  const url = req.url\n  const path = url.split(&#039;?&#039;)[0]\n  const queryStr = url.split(&#039;?&#039;)[1]\/\/ a=100&amp;b=200 \u975e\u7ed3\u6784\u5316\n  \/\/ console.log(&#039;queryStr&#039;,queryStr)\n  const method = req.method\n  \/\/ console.log(&#039;url is: &#039;,url)\/\/  \/api\/list?a=200\n  \/\/ console.log(&#039;method is: &#039;,method)\n  \/\/\u89e3\u6790 querystring\n  const query = {}\n  queryStr &amp;&amp; queryStr.split(&#039;&amp;&#039;).forEach(item =&gt; {\n    \/\/item \u5373 a=100 \u5f62\u5f0f\n    const key = item.split(&#039;=&#039;)[0]\/\/ &#039;a&#039;\n    const val = item.split(&#039;=&#039;)[1]\/\/ &#039;100&#039;\n    query[key] = val\/\/{a:&#039;100&#039;,b:&#039;200&#039;} \u7ed3\u6784\u5316\n  })\n  \/\/ console.log(&#039;query is&#039;, query)\n  \/\/\u5b9a\u4e49\u8def\u7531: \u6a21\u62df\u83b7\u53d6\u7559\u8a00\u677f\u5217\u8868\n  if (path === &#039;\/api\/list&#039; &amp;&amp; method === &#039;GET&#039;) {\n    if(query.filterType===&#039;1&#039;){\n      res.end(&#039;this is list router,all&#039;)\n    }\n    if(query.filterType===&#039;2&#039;){\n      res.end(&#039;this is list router,only mine&#039;)\n    }\n    \/\/ res.end(&#039;this is list router&#039;) \/\/res \u7684\u8fd4\u56de\n  } else { res.end(&#039;404&#039;) }\n  \/\/\u5b9a\u4e49\u8def\u7531:\u6a21\u62df\u521b\u5efa\u7559\u8a00\n  if (path === &#039;\/api\/create&#039; &amp;&amp; method === &#039;POST&#039;) {\n    res.end(&#039;this is create router&#039;)\n  }\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)<\/code><\/pre>\n<h2>querystring-hash \u4e0d\u80fd\u4f20\u5230\u670d\u52a1\u7aef<\/h2>\n<pre class=\"prettyprint linenums\" ><code> \/\/\u89e3\u6790 querystring\n  const query = {}\n  queryStr &amp;&amp; queryStr.split(&#039;&amp;&#039;).forEach(item =&gt; {\n    \/\/item \u5373 a=100 \u5f62\u5f0f\n    const key = item.split(&#039;=&#039;)[0]\/\/ &#039;a&#039;\n    const val = item.split(&#039;=&#039;)[1]\/\/ &#039;100&#039;\n    query[key] = val\/\/{a:&#039;100&#039;}\n  })\n===========================\nconst query=querystring.parse(queryStr)\n  console.log(&#039;query is &#039;,query)<\/code><\/pre>\n<h2>querystring-\u7ed3\u6784\u5316\u4e0e\u975e\u7ed3\u6784\u5316<\/h2>\n<ul>\n<li>\n<p>\u7ed3\u6784\u5316\u7684\u6570\u636e,\u6613\u4e8e\u901a\u8fc7\u7a0b\u5e8f\u8bbf\u95ee\u548c\u5206\u6790,\u5982\u5bf9\u8c61\u548c\u6570\u7ec4<\/p>\n<\/li>\n<li>\n<p>\u975e\u7ed3\u6784\u5316\u7684\u6570\u636e,\u4e0d\u6613\u901a\u8fc7\u7a0b\u5e8f\u5206\u6790,\u5982\u5b57\u7b26\u4e32<\/p>\n<\/li>\n<li>\n<p>\u7f16\u7a0b\u4e2d\u7684\u6570\u636e,\u90fd\u5c3d\u91cf\u7ed3\u6784\u5316<\/p>\n<ul>\n<li>a=100&amp;b=200 \u975e\u7ed3\u6784\u5316<\/li>\n<li>{ a : '100', b : '200' } \u7ed3\u6784\u5316<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>querystring \u662f url ? \u540e\u9762\u7684\u53c2\u6570<\/p>\n<\/li>\n<li>\n<p>\u52a8\u6001\u7f51\u9875\u7684\u57fa\u77f3 (hash \u5c31\u4e0d\u884c)<\/p>\n<\/li>\n<li>\n<p>nodejs \u5904\u7406 querystring,\u5e76\u5c06\u5176\u7ed3\u6784\u5316<\/p>\n<\/li>\n<\/ul>\n<h2>res \u8fd4\u56de\u6570\u636e-\u8fd4\u56de json \u683c\u5f0f<\/h2>\n<ul>\n<li>\u4f7f\u7528 res \u8bbe\u7f6e\u8fd4\u56de\u7684\u72b6\u6001\u7801,Content-type,Body<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\" ><code>\/\/\u6ca1\u6709\u547d\u4e2d\u8def\u7531,\u9ed8\u8ba4 404\n  \/\/ res.writeHead(404, { &#039;Content-type&#039;: &#039;text\/plain&#039; })\n  \/\/ res.end(&#039;404 Not Found&#039;)<\/code><\/pre>\n<h2>res \u8fd4\u56de\u6570\u636e-\u8fd4\u56de html \u683c\u5f0f<\/h2>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst querystring = require(&#039;querystring&#039;)\nconst server = http.createServer((req, res) =&gt; {\n  const url = req.url\n  const path = url.split(&#039;?&#039;)[0]\n  const queryStr = url.split(&#039;?&#039;)[1]\n  const method = req.method\n  const query = querystring.parse(queryStr)\n  \/\/\u5b9a\u4e49\u8def\u7531: \u6a21\u62df\u83b7\u53d6\u7559\u8a00\u677f\u5217\u8868\n  if (path === &#039;\/api\/list&#039; &amp;&amp; method === &#039;GET&#039;) {\n    \/\/ res.end(&#039;this is list router&#039;) \/\/res \u7684\u8fd4\u56de\n    \/\/\u8fd4\u56de\u7ed3\u679c\n    const result = {\n      errno: 0,\n      data: [\n        { user: &#039;\u5f20\u4e09&#039;, content: &#039;\u7559\u8a00 1&#039; },\n        { user: &#039;\u674e\u56db&#039;, content: &#039;\u7559\u8a00 2&#039; }\n      ]\n    }\n    res.writeHead(200, { &#039;Content-type&#039;: &#039;application\/json&#039; })\n    res.end(JSON.stringify(result))\n  }\n  \/\/\u5b9a\u4e49\u8def\u7531:\u6a21\u62df\u521b\u5efa\u7559\u8a00\n  if (path === &#039;\/api\/create&#039; &amp;&amp; method === &#039;POST&#039;) {\n    \/\/ res.end(&#039;this is create router&#039;)\n    const result = {\n      errno: 0,\n      message: &#039;\u521b\u5efa\u6210\u529f&#039;\n    }\n    res.writeHead(200, { &#039;Content-type&#039;: &#039;application\/json&#039; })\n    res.end(JSON.stringify(result))\n  }\n  \/\/\u6ca1\u6709\u547d\u4e2d\u8def\u7531,\u9ed8\u8ba4 404\n  \/\/ res.writeHead(404, { &#039;Content-type&#039;: &#039;text\/plain&#039; })\n  \/\/ res.end(&#039;404 Not Found&#039;)\n  res.writeHead(404, { &#039;Content-type&#039;: &#039;text\/html&#039; })\n  res.end(`\n  &lt;!DOCTYPE html&gt;\n  &lt;html&gt;\n    &lt;head&gt;\n    &lt;title&gt;404&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n      &lt;h1&gt;404 Not Found&lt;\/h1&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n`)\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)<\/code><\/pre>\n<h2>\u63a5\u6536 request body-\u6d41<\/h2>\n<ul>\n<li>\n<p>\u83b7\u53d6 Request Body<\/p>\n<ul>\n<li>\u6d41(stream)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0b\u8f7d <\/p>\n<ul>\n<li>source \u5373\u670d\u52a1\u7aef,dest \u5373\u5ba2\u6237\u7aef(\u524d\u7aef)<\/li>\n<li>\u6c34 \u5373 \u4e0b\u8f7d\u7684\u6570\u636e,\u6c34\u7ba1 \u5373 \u7f51\u7edc (\u5e26\u5bbd\u4e0d\u786e\u5b9a)<\/li>\n<li>\u4e0b\u8f7d\u4e0d\u4ec5\u6307\u5927\u6587\u4ef6,\u6cdb\u6307\u6240\u6709\u7684 Response Body<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6d4f\u89c8\u5668\u4f1a\u63a5\u6536 \u6d41(stream)\u6570\u636e<\/p>\n<ul>\n<li>\u670d\u52a1\u7aef res.end(...) , \u4f1a\u81ea\u52a8\u4ee5\u6d41\u7684\u5f62\u5f0f\u8fd4\u56de<\/li>\n<li>\u6d4f\u89c8\u5668\u4f1a\u8bc6\u522b\u5230 \u6d41 ,\u5e76\u6301\u7eed\u63a5\u6536\u4fe1\u606f(\u4f1a\u6709\u8fdb\u5ea6\u6761)<\/li>\n<li>\u5f85\u5168\u90e8\u63a5\u6536\u5b8c,\u518d\u505a\u663e\u793a\u6216\u5904\u7406 (\u89c6\u9891\u662f\u4e00\u6bb5\u4e00\u6bb5\u7684\u64ad\u653e)<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0a\u4f20<\/p>\n<ul>\n<li>source \u5373\u5ba2\u6237\u7aef(\u524d\u7aef) ,dest \u5373\u670d\u52a1\u7aef<\/li>\n<li>\u6c34 \u5373 \u4e0a\u4f20\u7684 \u6570\u636e,\u6c34\u7ba1 \u5373 \u7f51\u7edc (\u5e26\u5bbd\u4e0d\u786e\u5b9a)<\/li>\n<li>\u4e0a\u4f20\u4e0d\u4ec5\u6307\u5927\u6587\u4ef6,\u6cdb\u6307\u6240\u6709\u7684 Request Body<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u670d\u52a1\u7aef\u5982\u4f55\u63a5\u6536 \u6d41(stream) \u6570\u636e<\/p>\n<ul>\n<li>\u524d\u7aef\u4f7f\u7528 Ajax (Postman)\u63d0\u4ea4\u6570\u636e Request Body<\/li>\n<li>\u670d\u52a1\u7aef\u9700\u8981\u8bc6\u522b \u6d41 ,\u5e76\u63a5\u6536\u6570\u636e<\/li>\n<li>\u8fd8\u8981\u77e5\u9053\u4f55\u65f6\u624d\u80fd\u63a5\u6536\u5b8c\u6210<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u4ee3\u7801\u6f14\u793a<\/h2>\n<pre class=\"prettyprint linenums\" ><code>const http = require(&#039;http&#039;)\nconst { chunk } = require(&#039;lodash&#039;)\nconst querystring = require(&#039;querystring&#039;)\nconst server = http.createServer((req, res) =&gt; {\n  const url = req.url\n  const path = url.split(&#039;?&#039;)[0]\n  const queryStr = url.split(&#039;?&#039;)[1]\n  const method = req.method\n  const query = querystring.parse(queryStr)\n  \/\/\u5b9a\u4e49\u8def\u7531:\u6a21\u62df\u521b\u5efa\u7559\u8a00\n  if (path === &#039;\/api\/create&#039; &amp;&amp; method === &#039;POST&#039;) {\n    \/\/ res.end(&#039;this is create router&#039;)\n    const reqType=req.headers[&#039;content-type&#039;]\n    let bodyStr = &#039;&#039;\n    req.on(&#039;data&#039;, chunk =&gt; {\/\/\u670d\u52a1\u7aef\u600e\u4e48\u53bb\u8bc6\u522b &quot;\u6d41&quot;,\u5e76\u63a5\u6536\u6570\u636e\n      \/\/chunk \u5373&quot;\u6d41&quot;\u7684\u6bcf\u4e00\u6bb5\u6570\u636e\n      bodyStr = bodyStr + chunk.toString()\n    })\n    req.on(&#039;end&#039;, () =&gt; {\/\/\u670d\u52a1\u7aef\u600e\u4e48\u77e5\u9053\u6d41\u5b8c\u4e86\n      if(reqType===&#039;application\/json&#039;){\/\/json \u683c\u5f0f\n        const body=JSON.parse(bodyStr)\n        console.log(&#039;body is &#039;, body)\/\/\u5bf9\u8c61\u683c\u5f0f\n      }\n      res.end(&#039;\u63a5\u6536\u5b8c\u6210&#039;)\/\/\u5f02\u6b65\n    })\n    return\n  }\n  res.writeHead(404, { &#039;Content-type&#039;: &#039;text\/html&#039; })\n  res.end(`\n  &lt;!DOCTYPE html&gt;\n  &lt;html&gt;\n    &lt;head&gt;\n    &lt;title&gt;404&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n      &lt;h1&gt;404 Not Found&lt;\/h1&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n`)\n})\nserver.listen(3000) \/\/\u53ef\u4ee5\u76d1\u542c http \u8bf7\u6c42\nconsole.log(&#039;http \u8bf7\u6c42\u5df2\u7ecf\u88ab\u76d1\u542c,3000 \u7aef\u53e3, \u8bf7\u8bbf\u95ee http:\/\/localhost:3000&#039;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Node.js \u57fa\u7840\u5165\u95e8 nodejs \u662f\u4ec0\u4e48 \u4e00\u4e2a\u57fa\u4e8e Chrome V8 \u5f15\u64ce\u7684 Javascript \u8fd0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-3101","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/3101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3101"}],"version-history":[{"count":2,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/3101\/revisions"}],"predecessor-version":[{"id":4335,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/3101\/revisions\/4335"}],"wp:attachment":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}