{"id":15438,"date":"2025-04-18T21:57:41","date_gmt":"2025-04-18T13:57:41","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/15438.html"},"modified":"2025-04-18T21:57:41","modified_gmt":"2025-04-18T13:57:41","slug":"node-js-%e5%88%9b%e5%bb%ba%e6%9c%80%e5%9f%ba%e6%9c%ac%e7%9a%84-web-%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/15438.html","title":{"rendered":"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\n<ul>\n<li>Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 &#x1f680;&#x1f310;<\/li>\n<li>\n<ul>\n<li>\u4e00\u3001Node.js Web \u670d\u52a1\u5668\u6982\u8ff0<\/li>\n<li>\u4e8c\u3001\u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 &#x1f5a5;\ufe0f<\/li>\n<li>\n<ul>\n<li>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u5982\u4f55\u8fd0\u884c\u670d\u52a1\u5668 &#x1f4bb;<\/li>\n<li>\u56db\u3001\u5904\u7406\u4e0d\u540c\u8def\u7531\u7684\u8bf7\u6c42 &#x1f6a6;<\/li>\n<li>\n<ul>\n<li>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u4e94\u3001\u8fd4\u56de HTML \u9875\u9762\u548c\u9759\u6001\u6587\u4ef6 &#x1f4c4;<\/li>\n<li>\n<ul>\n<li>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>\u516d\u3001\u603b\u7ed3 &#x1f4dd;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 &#x1f680;&#x1f310;<\/h4>\n<p>\u5728 Web \u5f00\u53d1\u4e2d&#xff0c;\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684 Web \u670d\u52a1\u5668\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002Node.js \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5185\u7f6e\u6a21\u5757 http&#xff0c;\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Web \u670d\u52a1\u5668\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Node.js \u521b\u5efa\u4e00\u4e2a\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668&#xff0c;\u5e76\u6df1\u5165\u4e86\u89e3\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u3002<\/p>\n<hr \/>\n<h5>\u4e00\u3001Node.js Web \u670d\u52a1\u5668\u6982\u8ff0<\/h5>\n<p>Node.js \u662f\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u548c\u975e\u963b\u585e I\/O \u6a21\u578b\u7684&#xff0c;\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684 http \u6a21\u5757&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u521b\u5efa Web \u5e94\u7528\u7a0b\u5e8f&#xff0c;\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u5e76\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<hr \/>\n<h5>\u4e8c\u3001\u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 &#x1f5a5;\ufe0f<\/h5>\n<p>\u9996\u5148&#xff0c;\u6211\u4eec\u9700\u8981\u5f15\u5165 http \u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668&#xff0c;\u5e76\u4f7f\u7528\u8be5\u670d\u52a1\u5668\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b&#xff0c;\u5c55\u793a\u4e86\u5982\u4f55\u7528 Node.js \u521b\u5efa\u4e00\u4e2a\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u5f15\u5165 http \u6a21\u5757<\/span><br \/>\n<span class=\"token keyword\">const<\/span> http <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;http&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u521b\u5efa HTTP \u670d\u52a1\u5668<\/span><br \/>\n<span class=\"token keyword\">const<\/span> server <span class=\"token operator\">&#061;<\/span> http<span class=\"token punctuation\">.<\/span><span class=\"token function\">createServer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934<\/span><br \/>\n  res<span class=\"token punctuation\">.<\/span><span class=\"token function\">writeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">200<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token string-property property\">&#039;Content-Type&#039;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#039;text\/plain&#039;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9<\/span><br \/>\n  res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;Hello, World!&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3 3000<\/span><br \/>\nserver<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u670d\u52a1\u5668\u6b63\u5728\u76d1\u542c\u7aef\u53e3 3000&#8230;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h6>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/h6>\n<li>require(&#039;http&#039;)&#xff1a;\u5f15\u5165 Node.js \u5185\u7f6e\u7684 http \u6a21\u5757&#xff0c;\u8fd9\u4e2a\u6a21\u5757\u7528\u4e8e\u521b\u5efa Web \u670d\u52a1\u5668\u3002<\/li>\n<li>http.createServer()&#xff1a;\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668&#xff0c;\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u51fd\u6570&#xff0c;\u56de\u8c03\u51fd\u6570\u7684\u4e24\u4e2a\u53c2\u6570\u662f req&#xff08;\u8bf7\u6c42\u5bf9\u8c61&#xff09;\u548c res&#xff08;\u54cd\u5e94\u5bf9\u8c61&#xff09;\u3002<\/li>\n<li>res.writeHead(200, { &#039;Content-Type&#039;: &#039;text\/plain&#039; })&#xff1a;\u8bbe\u7f6e\u54cd\u5e94\u5934&#xff0c;200 \u662f HTTP \u72b6\u6001\u7801&#xff0c;\u8868\u793a\u8bf7\u6c42\u6210\u529f&#xff0c;Content-Type \u8bbe\u7f6e\u4e3a text\/plain \u8868\u793a\u8fd4\u56de\u7684\u662f\u7eaf\u6587\u672c\u3002<\/li>\n<li>res.end(&#039;Hello, World!&#039;)&#xff1a;\u7ed3\u675f\u54cd\u5e94\u5e76\u8fd4\u56de\u5185\u5bb9 &#034;Hello, World!&#034; \u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<li>server.listen(3000)&#xff1a;\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u76d1\u542c\u7aef\u53e3 3000&#xff0c;\u5f53\u670d\u52a1\u5668\u6210\u529f\u542f\u52a8\u65f6&#xff0c;\u56de\u8c03\u51fd\u6570\u4f1a\u6267\u884c&#xff0c;\u6253\u5370\u51fa\u4e00\u6761\u4fe1\u606f&#xff0c;\u8868\u793a\u670d\u52a1\u5668\u6b63\u5728\u76d1\u542c\u3002<\/li>\n<h5>\u4e09\u3001\u5982\u4f55\u8fd0\u884c\u670d\u52a1\u5668 &#x1f4bb;<\/h5>\n<li>\n<p>\u4fdd\u5b58\u4e0a\u9762\u7684\u4ee3\u7801\u4e3a server.js \u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d&#xff0c;\u8fdb\u5165\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u670d\u52a1\u5668&#xff1a;<\/p>\n<p> <span class=\"token function\">node<\/span> server.js\n <\/li>\n<li>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00 http:\/\/localhost:3000&#xff0c;\u4f60\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\u6d4f\u89c8\u5668\u663e\u793a \u201cHello, World!\u201d\u3002<\/p>\n<\/li>\n<hr \/>\n<h5>\u56db\u3001\u5904\u7406\u4e0d\u540c\u8def\u7531\u7684\u8bf7\u6c42 &#x1f6a6;<\/h5>\n<p>\u867d\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Web \u670d\u52a1\u5668&#xff0c;\u4f46\u6211\u4eec\u901a\u5e38\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684 URL \u8def\u5f84\u6765\u5904\u7406\u4e0d\u540c\u7684\u8bf7\u6c42\u3002\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c06\u6269\u5c55\u4e00\u4e0b&#xff0c;\u5904\u7406\u4e0d\u540c\u7684\u8def\u7531\u3002<\/p>\n<p><span class=\"token keyword\">const<\/span> http <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;http&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u521b\u5efa HTTP \u670d\u52a1\u5668<\/span><br \/>\n<span class=\"token keyword\">const<\/span> server <span class=\"token operator\">&#061;<\/span> http<span class=\"token punctuation\">.<\/span><span class=\"token function\">createServer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934<\/span><br \/>\n  res<span class=\"token punctuation\">.<\/span><span class=\"token function\">writeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">200<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token string-property property\">&#039;Content-Type&#039;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#039;text\/plain&#039;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u5224\u65ad\u8bf7\u6c42\u8def\u5f84&#xff0c;\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94<\/span><br \/>\n  <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>req<span class=\"token punctuation\">.<\/span>url <span class=\"token operator\">&#061;&#061;&#061;<\/span> <span class=\"token string\">&#039;\/&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u6b22\u8fce\u6765\u5230\u9996\u9875&#xff01;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>req<span class=\"token punctuation\">.<\/span>url <span class=\"token operator\">&#061;&#061;&#061;<\/span> <span class=\"token string\">&#039;\/about&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u8fd9\u662f\u5173\u4e8e\u9875\u9762&#xff01;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u627e\u4e0d\u5230\u9875\u9762&#xff01;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3 3000<\/span><br \/>\nserver<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u670d\u52a1\u5668\u6b63\u5728\u76d1\u542c\u7aef\u53e3 3000&#8230;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h6>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/h6>\n<li>req.url&#xff1a;\u8bf7\u6c42\u5bf9\u8c61\u7684 url \u5c5e\u6027\u5305\u542b\u4e86\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u8def\u5f84\u90e8\u5206\u3002<\/li>\n<li>if-else \u8bed\u53e5&#xff1a;\u6839\u636e\u4e0d\u540c\u7684\u8def\u5f84\u8fd4\u56de\u4e0d\u540c\u7684\u5185\u5bb9\u3002\u5982\u679c\u8bf7\u6c42\u7684\u662f \/ \u8def\u5f84&#xff0c;\u8fd4\u56de\u9996\u9875\u5185\u5bb9&#xff1b;\u5982\u679c\u8bf7\u6c42\u7684\u662f \/about&#xff0c;\u8fd4\u56de\u5173\u4e8e\u9875\u9762\u5185\u5bb9&#xff1b;\u5426\u5219\u8fd4\u56de 404 \u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<h5>\u4e94\u3001\u8fd4\u56de HTML \u9875\u9762\u548c\u9759\u6001\u6587\u4ef6 &#x1f4c4;<\/h5>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4ec5\u4ec5\u662f\u5904\u7406\u6587\u672c\u6570\u636e&#xff0c;\u4f46\u5728\u5b9e\u9645 Web \u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u9700\u8981\u8fd4\u56de\u66f4\u590d\u6742\u7684 HTML \u9875\u9762\u6216\u9759\u6001\u6587\u4ef6&#xff08;\u5982\u56fe\u7247\u3001CSS\u3001JavaScript \u6587\u4ef6\u7b49&#xff09;\u3002\u8fd9\u91cc&#xff0c;\u6211\u4eec\u6765\u6269\u5c55\u4e00\u4e0b\u4ee3\u7801&#xff0c;\u4f7f\u5176\u8fd4\u56de HTML \u5185\u5bb9\u3002<\/p>\n<p><span class=\"token keyword\">const<\/span> http <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;http&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> fs <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;fs&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> path <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;path&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u521b\u5efa HTTP \u670d\u52a1\u5668<\/span><br \/>\n<span class=\"token keyword\">const<\/span> server <span class=\"token operator\">&#061;<\/span> http<span class=\"token punctuation\">.<\/span><span class=\"token function\">createServer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">req<span class=\"token punctuation\">,<\/span> res<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u83b7\u53d6\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84<\/span><br \/>\n  <span class=\"token keyword\">let<\/span> filePath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;.&#039;<\/span> <span class=\"token operator\">&#043;<\/span> req<span class=\"token punctuation\">.<\/span>url<span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u9ed8\u8ba4\u8fd4\u56de index.html \u6587\u4ef6<\/span><br \/>\n  <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>filePath <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;.\/&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    filePath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;.\/index.html&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u6587\u4ef6\u6269\u5c55\u540d<\/span><br \/>\n  <span class=\"token keyword\">const<\/span> extname <span class=\"token operator\">&#061;<\/span> path<span class=\"token punctuation\">.<\/span><span class=\"token function\">extname<\/span><span class=\"token punctuation\">(<\/span>filePath<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934<\/span><br \/>\n  <span class=\"token keyword\">let<\/span> contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;text\/html&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u6839\u636e\u6587\u4ef6\u6269\u5c55\u540d\u8bbe\u7f6e\u4e0d\u540c\u7684 Content-Type<\/span><br \/>\n  <span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">(<\/span>extname<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.js&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;application\/javascript&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.css&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;text\/css&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.json&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;application\/json&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.png&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;image\/png&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.jpg&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;image\/jpeg&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">case<\/span> <span class=\"token string\">&#039;.gif&#039;<\/span><span class=\"token operator\">:<\/span><br \/>\n      contentType <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;image\/gif&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u8bfb\u53d6\u6587\u4ef6\u5e76\u8fd4\u56de<\/span><br \/>\n  fs<span class=\"token punctuation\">.<\/span><span class=\"token function\">readFile<\/span><span class=\"token punctuation\">(<\/span>filePath<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">err<span class=\"token punctuation\">,<\/span> content<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>err<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      res<span class=\"token punctuation\">.<\/span><span class=\"token function\">writeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">404<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token string-property property\">&#039;Content-Type&#039;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#039;text\/plain&#039;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u6587\u4ef6\u672a\u627e\u5230&#xff01;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n      res<span class=\"token punctuation\">.<\/span><span class=\"token function\">writeHead<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">200<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token string-property property\">&#039;Content-Type&#039;<\/span><span class=\"token operator\">:<\/span> contentType <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n      res<span class=\"token punctuation\">.<\/span><span class=\"token function\">end<\/span><span class=\"token punctuation\">(<\/span>content<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3 3000<\/span><br \/>\nserver<span class=\"token punctuation\">.<\/span><span class=\"token function\">listen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;\u670d\u52a1\u5668\u6b63\u5728\u76d1\u542c\u7aef\u53e3 3000&#8230;&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h6>\u89e3\u91ca\u4ee3\u7801&#xff1a;<\/h6>\n<li>fs.readFile(filePath, callback)&#xff1a;\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<li>path.extname(filePath)&#xff1a;\u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d&#xff0c;\u5e2e\u52a9\u6211\u4eec\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u3002<\/li>\n<li>contentType&#xff1a;\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u54cd\u5e94\u5934\u4e2d\u7684 Content-Type&#xff0c;\u4f8b\u5982 HTML \u6587\u4ef6\u8bbe\u7f6e\u4e3a text\/html&#xff0c;JavaScript \u6587\u4ef6\u8bbe\u7f6e\u4e3a application\/javascript\u3002<\/li>\n<li>\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728&#xff0c;\u4f1a\u8fd4\u56de 404 \u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<h5>\u516d\u3001\u603b\u7ed3 &#x1f4dd;<\/h5>\n<p>\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0&#xff0c;\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528 Node.js \u521b\u5efa\u4e00\u4e2a\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668&#xff1a;<\/p>\n<li>\u521b\u5efa Web \u670d\u52a1\u5668&#xff1a;\u901a\u8fc7 http.createServer() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668&#xff0c;\u5e76\u4f7f\u7528 listen() \u65b9\u6cd5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u3002<\/li>\n<li>\u5904\u7406\u8bf7\u6c42&#xff1a;\u6839\u636e req.url \u6765\u5224\u65ad\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84&#xff0c;\u5e76\u6839\u636e\u8def\u5f84\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u5185\u5bb9\u3002<\/li>\n<li>\u8fd4\u56de\u9759\u6001\u6587\u4ef6&#xff1a;\u901a\u8fc7 fs \u6a21\u5757\u8bfb\u53d6 HTML\u3001CSS\u3001JS \u7b49\u9759\u6001\u6587\u4ef6&#xff0c;\u5e76\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u5408\u9002\u7684\u54cd\u5e94\u5934\u3002<\/li>\n<p>Node.js \u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\u6765\u5904\u7406 HTTP \u8bf7\u6c42&#xff0c;\u9002\u5408\u7528\u6765\u5feb\u901f\u5f00\u53d1 Web \u670d\u52a1\u5668\u3002\u968f\u7740\u9700\u6c42\u7684\u590d\u6742\u5316&#xff0c;\u60a8\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u6269\u5c55\u66f4\u591a\u7684\u529f\u80fd&#xff0c;\u4f8b\u5982\u8def\u7531\u7ba1\u7406\u3001\u8bf7\u6c42\u5904\u7406\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb804\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Node.js \u662f\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u548c\u975e\u963b\u585e I\/O \u6a21\u578b\u7684\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684http\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u521b\u5efa Web \u5e94\u7528\u7a0b\u5e8f\uff0c\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u5e76\u8fd4\u56de\u6570\u636e\u3002\u521b\u5efa Web \u670d\u52a1\u5668\uff1a\u901a\u8fc7\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528listen()\u65b9\u6cd5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u3002\u5904\u7406\u8bf7\u6c42\uff1a\u6839\u636ereq.url\u6765\u5224\u65ad\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u5e76\u6839\u636e\u8def\u5f84\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u5185\u5bb9\u3002\u8fd4\u56de\u9759\u6001\u6587\u4ef6\uff1a\u901a\u8fc7fs\u6a21\u5757\u8bfb\u53d6 HTML\u3001CSS\u3001JS \u7b49\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u5408\u9002\u7684\u54cd\u5e94\u5934\u3002_\u4f7f\u7528node\u521b\u5efa\u670d\u52a1\u5668\u7684\u65b9\u5f0f\u6709\u51e0\u79cd,\u533a\u522b\u662f\u4ec0\u4e48<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[965,964,187,186,87,43,727,44],"topic":[],"class_list":["post-15438","post","type-post","status-publish","format-standard","hentry","category-server","tag-firefox","tag-html5","tag-javascript","tag-node-js","tag-87","tag-43","tag-727","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/15438.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb804\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Node.js \u662f\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u548c\u975e\u963b\u585e I\/O \u6a21\u578b\u7684\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684http\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u521b\u5efa Web \u5e94\u7528\u7a0b\u5e8f\uff0c\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u5e76\u8fd4\u56de\u6570\u636e\u3002\u521b\u5efa Web \u670d\u52a1\u5668\uff1a\u901a\u8fc7\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528listen()\u65b9\u6cd5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u3002\u5904\u7406\u8bf7\u6c42\uff1a\u6839\u636ereq.url\u6765\u5224\u65ad\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u5e76\u6839\u636e\u8def\u5f84\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u5185\u5bb9\u3002\u8fd4\u56de\u9759\u6001\u6587\u4ef6\uff1a\u901a\u8fc7fs\u6a21\u5757\u8bfb\u53d6 HTML\u3001CSS\u3001JS \u7b49\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u5408\u9002\u7684\u54cd\u5e94\u5934\u3002_\u4f7f\u7528node\u521b\u5efa\u670d\u52a1\u5668\u7684\u65b9\u5f0f\u6709\u51e0\u79cd,\u533a\u522b\u662f\u4ec0\u4e48\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/15438.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T13:57:41+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15438.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/15438.html\",\"name\":\"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T13:57:41+00:00\",\"dateModified\":\"2025-04-18T13:57:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/15438.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/15438.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/15438.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/15438.html","og_locale":"zh_CN","og_type":"article","og_title":"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb804\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Node.js \u662f\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u548c\u975e\u963b\u585e I\/O \u6a21\u578b\u7684\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684http\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u521b\u5efa Web \u5e94\u7528\u7a0b\u5e8f\uff0c\u54cd\u5e94\u5ba2\u6237\u7aef\u8bf7\u6c42\u5e76\u8fd4\u56de\u6570\u636e\u3002\u521b\u5efa Web \u670d\u52a1\u5668\uff1a\u901a\u8fc7\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528listen()\u65b9\u6cd5\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\u3002\u5904\u7406\u8bf7\u6c42\uff1a\u6839\u636ereq.url\u6765\u5224\u65ad\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u5e76\u6839\u636e\u8def\u5f84\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u5185\u5bb9\u3002\u8fd4\u56de\u9759\u6001\u6587\u4ef6\uff1a\u901a\u8fc7fs\u6a21\u5757\u8bfb\u53d6 HTML\u3001CSS\u3001JS \u7b49\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u5408\u9002\u7684\u54cd\u5e94\u5934\u3002_\u4f7f\u7528node\u521b\u5efa\u670d\u52a1\u5668\u7684\u65b9\u5f0f\u6709\u51e0\u79cd,\u533a\u522b\u662f\u4ec0\u4e48","og_url":"https:\/\/www.wsisp.com\/helps\/15438.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T13:57:41+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/15438.html","url":"https:\/\/www.wsisp.com\/helps\/15438.html","name":"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T13:57:41+00:00","dateModified":"2025-04-18T13:57:41+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/15438.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/15438.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/15438.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Node.js \u521b\u5efa\u6700\u57fa\u672c\u7684 Web \u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/15438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=15438"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/15438\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=15438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=15438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=15438"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=15438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}