{"id":4477,"date":"2015-11-22T00:25:59","date_gmt":"2015-11-21T16:25:59","guid":{"rendered":"https:\/\/sdeno.com\/?p=4477"},"modified":"2016-07-04T20:48:22","modified_gmt":"2016-07-04T12:48:22","slug":"nodejs%e8%b6%85%e7%ae%80%e5%8d%95web%e7%bd%91%e7%ab%99%e5%8f%91%e5%b8%83%e5%99%a8","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=4477","title":{"rendered":"nodejs\u8d85\u7b80\u5355web\u7f51\u7ad9\u53d1\u5e03\u5668"},"content":{"rendered":"<p>node iis apache<\/p>\n<p>\u4e4b\u524d\u6709\u4ecb\u7ecd\u8fc7\u9760express\u6846\u67b6\u5f00\u53d1\u4ee5\u53ca\u53d1\u5e03\u7f51\u7ad9\u300a<a href=\"https:\/\/sdeno.com\/?p=4425\" target=\"_blank\">nodejs\u7b80\u5355\u642d\u5efa\u7f51\u7ad9<\/a>\u300b\uff0c\u8fd9\u91cc\u6211\u4eec\u5728\u4ece\u5934\u5f00\u59cb\u7528nodejs\u539f\u751f\u8bed\u6cd5\u6765\u5b66\u4e60\u7b80\u5355\u53d1\u5e03\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u7f51\u7ad9<\/p>\n<p>&nbsp;<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a1.js\u7684\u6587\u4ef6\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801<\/p>\n<pre>var http=require(\"http\"); \/\/http\u662fnodejs\u63d0\u4f9b\u7684\u6a21\u5757\r\nhttp.createServer(function(req, res){ \/\/(\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u6d4f\u89c8\u5668\u63a5\u53d7\u54cd\u5e94)\r\n res.writeHead(200, { 'Content-Type' : 'text\/plain;charset=utf-8'}); \/\/\u5411\u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u53d1\u9001\u54cd\u5e94\u5934\u3002\r\n res.end('Hello World\\n'); \/\/\u6700\u76f4\u63a5\u7b80\u5355\u7684\u5411\u6d4f\u89c8\u5668\u8f93\u51fa\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u88f9\u5728&lt;pre&gt;\u6807\u7b7e\u4e2d\r\n}).listen(8124,\"127.0.0.1\");<\/pre>\n<p>\u4e4b\u540e\u5728\u547d\u4ee4\u91cc\u9762\u8f93\u5165node 1.js<\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\uff1alocalhost:8124 \u00a0\u5c31\u80fd\u8bbf\u95ee\u5230\u6211\u4eec\u521a\u521a\u5411\u6d4f\u89c8\u5668\u8f93\u51fa\u7684\u4fe1\u606f\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728\u5f15\u5165\u4e00\u4e2afs\u6a21\u5757<\/p>\n<pre style=\"color: #000000;\"><span style=\"font-weight: bold; color: #000080;\">var <\/span><span style=\"font-weight: bold; font-style: italic; color: #660e7a;\">http<\/span>=<span style=\"font-weight: bold; color: #660e7a;\">require<\/span>(<span style=\"font-weight: bold; color: #008000;\">\"http\"<\/span>); <span style=\"font-style: italic; color: #808080;\">\/\/http\u662fnodejs\u81ea\u5e26\u7684\u6a21\u5757\r\n<\/span><span style=\"font-weight: bold; color: #000080;\">var <\/span><span style=\"font-weight: bold; font-style: italic; color: #660e7a;\">fs <\/span>= <span style=\"font-weight: bold; color: #660e7a;\">require<\/span>(<span style=\"font-weight: bold; color: #008000;\">'fs'<\/span>) <span style=\"font-style: italic; color: #808080;\">\/\/\u81ea\u5e26\r\n<\/span><span style=\"font-weight: bold; font-style: italic; color: #660e7a;\">http<\/span>.createServer(<span style=\"font-weight: bold; color: #000080;\">function<\/span>(req, res){  <span style=\"font-style: italic; color: #808080;\">\/\/(\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u6d4f\u89c8\u5668\u63a5\u53d7\u54cd\u5e94)\r\n<\/span>res.<span style=\"font-weight: bold; color: #660e7a;\">writeHead<\/span>(<span style=\"color: #0000ff;\">200<\/span>, { <span style=\"font-weight: bold; color: #660e7a;\">'Content-Type' <\/span>: <span style=\"font-weight: bold; color: #008000;\">'text\/plain;charset=utf-8'<\/span>}); <span style=\"font-style: italic; color: #808080;\">\/\/\u5411\u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u53d1\u9001\u54cd\u5e94\u5934\u3002\r\n<\/span><span style=\"font-style: italic; color: #808080;\">   \/\/ res.end('Hello World\\n');\r\n<\/span><span style=\"font-style: italic; color: #808080;\">\r\n\/\/\/\/\u83b7\u53d61.txt\u6587\u4ef6\uff0c\u4ee5\u4ec0\u4e48\u5b57\u7b26\u96c6\u4f20\u7ed9\u6d4f\u89c8\u5668\uff0c\u56de\u8c03(\u9519\u8bef\u4fe1\u606f\uff0c\u6587\u4ef6\u5185\u5bb9)\r\n<\/span><span style=\"font-weight: bold; font-style: italic; color: #660e7a;\">fs<\/span>.readFile(<span style=\"font-weight: bold; color: #008000;\">\"1.txt\"<\/span>, <span style=\"font-weight: bold; color: #008000;\">\"UTF-8\"<\/span>, <span style=\"font-weight: bold; color: #000080;\">function<\/span>(err, file) {\r\n        <span style=\"font-weight: bold; color: #000080;\">if<\/span>(err) {\r\n            <span style=\"font-weight: bold; color: #660e7a;\">console<\/span>.<span style=\"color: #7a7a43;\">log<\/span>(err);\r\n            <span style=\"font-weight: bold; color: #000080;\">return<\/span>;\r\n        }\r\n        <span style=\"font-weight: bold; color: #660e7a;\">fileContent <\/span>= file;\r\n        res.<span style=\"color: #7a7a43;\">end<\/span>(<span style=\"font-weight: bold; color: #660e7a;\">fileContent<\/span>); \/\/\u8f93\u51fa1.txt\u91cc\u9762\u7684\u5185\u5bb9\r\n    });\r\n\r\n}).<span style=\"color: #7a7a43;\">listen<\/span>(<span style=\"color: #0000ff;\">8124<\/span>,<span style=\"font-weight: bold; color: #008000;\">\"127.0.0.1\"<\/span>);<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>node iis apache \u4e4b\u524d\u6709\u4ecb\u7ecd\u8fc7\u9760express\u6846\u67b6\u5f00\u53d1\u4ee5\u53ca\u53d1\u5e03\u7f51\u7ad9\u300anodejs\u7b80\u5355\u642d\u5efa\u7f51\u7ad9\u300b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,11],"tags":[],"class_list":["post-4477","post","type-post","status-publish","format-standard","hentry","category-nodejs","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4477"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4477\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}