Node.js\u662f\u7528\u4e8e\u6267\u884c\u670d\u52a1\u5668\u7aefJavaScript\u4ee3\u7801\u5e7f\u6cdb\u4f7f\u7528\u7684\u5f00\u6e90\u548c\u8de8\u5e73\u53f0JavaScript\u8fd0\u884c\u65f6\u73af\u5883\uff0cNode.js\u516c\u53f8NodeSource\u4eca\u5929\u5ba3\u5e03\uff0c\u4ed6\u4eec\u5236\u4f5c\u4e86\u4e00\u4e2aSnap\u8f6f\u4ef6\u5305\uff0c\u4ee5\u4fbfLinux\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u66f4\u8f7b\u677e\u5730\u5728\u5176\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5\u6d41\u884c\u7684JavaScript\u8fd0\u884c\u65f6\u73af\u5883\u3002 Snap\u662f\u7531Canonical\u4e3aUbuntu Linux\u5f00\u53d1\u7684\u4e00\u79cd\u96c6\u88c5\u7bb1\u5316\u7684\u901a\u7528\u4e8c\u8fdb\u5236\u5305\u683c\u5f0f\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u636eStack Overflow 2018\u5f00\u53d1\u8005\u8c03\u67e5\u62a5\u544a\uff0cLinux\u5f00\u53d1\u4eba\u5458\u7684\u9996\u9009\u5f00\u53d1\u5e73\u53f0\uff0c\u90a3\u4e48\u5bf9\u4e8e\u8fd0\u884c\u60a8\u6700\u559c\u7231\u7684\u7f16\u7a0b\u8bed\u8a00\u3001\u6846\u67b6\u548c\u5f00\u53d1\u73af\u5883\u7684\u6700\u65b0\u7248\u672c\u7684\u9700\u6c42\u5df2\u7ecf\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\uff0c\u800cCanonical\u7684Snappy\u6280\u672f\u5c31\u662f\u7b54\u6848\u3002<\/p>\n
\u201cNode.js snap\u5305\u542b\u4e86Node.js\u8fd0\u884c\u65f6\u4ee5\u53ca\u5e7f\u6cdb\u4f7f\u7528\u7684\u5305\u7ba1\u7406\u5668npm\uff0c\u56e0\u6b64\u53ea\u9700\u4e00\u4e2a\u547d\u4ee4\uff0c\u5f00\u53d1\u4eba\u5458\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ed6\u4eec\u9009\u62e9\u7684Node.js\u7248\u672c\uff0c\u4e0d\u9700\u8981\u5916\u90e8\u7684repos\u6216\u4e2a\u4eba\u5305\u6863\u6848(PPAs)\uff0c\u201dNodeSource\u8bf4\u3002<\/p>\n
\u4f7f\u7528Snap\u8f6f\u4ef6\u5305\u7684\u597d\u5904\u662f\u663e\u800c\u6613\u89c1\u7684\uff0c\u56e0\u4e3a\u4e00\u65e6\u5b83\u4e0a\u6e38\u53d1\u5e03\uff0c\u60a8\u5c31\u4f1a\u59cb\u7ec8\u6536\u5230\u6700\u65b0\u7684Node.js\u7248\u672c\u3002\u501f\u52a9Canonical\u7684Snappy\u6280\u672f\uff0cNodeSource\u53ef\u4ee5\u5728\u51e0\u5c0f\u65f6\u6216\u51e0\u5206\u949f\u5185\u5c06\u6700\u65b0\u7684Node.js\u7248\u672c\u63d0\u4f9b\u7ed9\u652f\u6301Snap\u7684Linux\u7cfb\u7edf\u3002<\/p>\n
\u4ee5\u4e0b\u662f\u5982\u4f55\u5728Ubuntu\u4ee5\u53ca\u5176\u4ed6Linux\u53d1\u884c\u7248\u5c06Node.js\u4f7f\u7528Snap\u5305<\/p>\n
\u8981\u5728\u542f\u7528Snap\u7684GNU\/Linux\u53d1\u884c\u7248\u4e0a\u5b89\u88c5Node.js\uff0c\u60a8\u53ea\u9700\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u5373\u53ef\u3002 Canonical\u7684Snappy\u6280\u672f\u76ee\u524d\u652f\u6301Ubuntu\uff0cDebian\uff0cLinux Mint\uff0cFedora\uff0cArch Linux\uff0cOpenSUSE\uff0cSolus\uff0cGentoo Linux\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u5d4c\u5165\u5f0f\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5982OpenWrt\u548cOpenEmbedded\u3002<\/p>\n
sudo snap install node --classic --channel=8\/stable\r\n<\/pre>\n\u5982\u4e0b\u56fe\uff1a<\/p>\n
<\/p>\n
\u7531\u4e8eNodeSource\u7ef4\u62a4\u4e86\u5176Node.js JavaScript\u8fd0\u884c\u65f6\u73af\u5883\u7684\u591a\u4e2a\u7248\u672c\uff0c\u56e0\u6b64\u5b83\u4eec\u8fd8\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u66f4\u6539\u9891\u9053\uff0c\u5e76\u4e14\u5982\u679c\u60a8\u89c9\u5f97\u5192\u9669\uff0c\u5219\u53ef\u4ee5\u5b89\u88c5\u6700\u65b0\u7684\u6d41\u884c\u7248\u672c\u3002\u4f7f\u7528\u4e0b\u9762\u5217\u51fa\u7684\u7b2c\u4e00\u7ec4\u547d\u4ee4\u66f4\u6539\u901a\u9053<\/p>\n
sudo snap switch node --channel=9\/stable\r\nsudo snap refresh\r\n<\/pre>\n\u7b2c\u4e8c\u7ec4\u5b89\u88c5\u6700\u524d\u6cbf\u7248\u672c\u7684\u547d\u4ee4\u3002<\/p>\n
sudo snap install node --classic --channel=edge\r\n<\/pre>\n\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u770b\u770b\u7248\u672c\uff1a<\/p>\n
node -v\r\n<\/pre>\nv6.11.4<\/p>\n
\u89c1\u4e0b\u56fe\uff1a<\/p>\n
<\/p>\n
\u7b2c\u4e00\u4e2aNode.js\u7a0b\u5e8f\uff1aHello World\uff01<\/p>\n
\u811a\u672c\u6a21\u5f0f<\/p>\n
\u4ee5\u4e0b\u662f\u6211\u4eec\u7684\u7b2c\u4e00\u4e2aNode.js\u7a0b\u5e8f\uff1a<\/p>\n
\u5b9e\u4f8b<\/p>\n
console.log(\"Hello World\");<\/p>\n
console.log(\"Hello World www.linuxidc.com\");<\/p>\n
\u770b\u770b\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n
<\/p>\n
\u521b\u5efa\u7b2c\u4e00\u4e2aNode.js\u5e94\u7528
\n\u5982\u679c\u6211\u4eec\u4f7f\u7528PHP\u6765\u7f16\u5199\u540e\u7aef\u7684\u4ee3\u7801\u65f6\uff0c\u9700\u8981Apache \u6216\u8005 Nginx \u7684HTTP \u670d\u52a1\u5668\uff0c\u5e76\u914d\u4e0a mod_php5 \u6a21\u5757\u548cphp-cgi\u3002<\/p>\n
\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u6574\u4e2a\"\u63a5\u6536 HTTP \u8bf7\u6c42\u5e76\u63d0\u4f9b Web \u9875\u9762\"\u7684\u9700\u6c42\u6839\u672c\u4e0d\u9700 \u8981 PHP \u6765\u5904\u7406\u3002<\/p>\n
\u4e0d\u8fc7\u5bf9 Node.js \u6765\u8bf4\uff0c\u6982\u5ff5\u5b8c\u5168\u4e0d\u4e00\u6837\u4e86\u3002\u4f7f\u7528 Node.js \u65f6\uff0c\u6211\u4eec\u4e0d\u4ec5\u4ec5 \u5728\u5b9e\u73b0\u4e00\u4e2a\u5e94\u7528\uff0c\u540c\u65f6\u8fd8\u5b9e\u73b0\u4e86\u6574\u4e2a HTTP \u670d\u52a1\u5668\u3002\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u7684 Web \u5e94\u7528\u4ee5\u53ca\u5bf9\u5e94\u7684 Web \u670d\u52a1\u5668\u57fa\u672c\u4e0a\u662f\u4e00\u6837\u7684\u3002<\/p>\n
\u5728\u6211\u4eec\u521b\u5efa Node.js \u7b2c\u4e00\u4e2a \"Hello, World!\" \u5e94\u7528\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u4e86\u89e3\u4e0b Node.js \u5e94\u7528\u662f\u7531\u54ea\u51e0\u90e8\u5206\u7ec4\u6210\u7684\uff1a<\/p>\n
1.\u5f15\u5165 required \u6a21\u5757\uff1a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 require \u6307\u4ee4\u6765\u8f7d\u5165 Node.js \u6a21\u5757\u3002<\/p>\n
2.\u521b\u5efa\u670d\u52a1\u5668\uff1a\u670d\u52a1\u5668\u53ef\u4ee5\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u7c7b\u4f3c\u4e8e Apache \u3001Nginx \u7b49 HTTP \u670d\u52a1\u5668\u3002<\/p>\n
3.\u63a5\u6536\u8bf7\u6c42\u4e0e\u54cd\u5e94\u8bf7\u6c42 \u670d\u52a1\u5668\u5f88\u5bb9\u6613\u521b\u5efa\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u6d4f\u89c8\u5668\u6216\u7ec8\u7aef\u53d1\u9001 HTTP \u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u63a5\u6536\u8bf7\u6c42\u540e\u8fd4\u56de\u54cd\u5e94\u6570\u636e\u3002<\/p>\n
\u521b\u5efa Node.js \u5e94\u7528<\/p>\n
\u6b65\u9aa4\u4e00\u3001\u5f15\u5165 required \u6a21\u5757<\/p>\n
\u6211\u4eec\u4f7f\u7528 require \u6307\u4ee4\u6765\u8f7d\u5165 http \u6a21\u5757\uff0c\u5e76\u5c06\u5b9e\u4f8b\u5316\u7684 HTTP \u8d4b\u503c\u7ed9\u53d8\u91cf http\uff0c\u5b9e\u4f8b\u5982\u4e0b:
\nvar http = require(\"http\");<\/p>\n
\u6b65\u9aa4\u4e8c\u3001\u521b\u5efa\u670d\u52a1\u5668<\/p>\n
\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528 http.createServer() \u65b9\u6cd5\u521b\u5efa\u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528 listen \u65b9\u6cd5\u7ed1\u5b9a 8888 \u7aef\u53e3\u3002 \u51fd\u6570\u901a\u8fc7 request, response \u53c2\u6570\u6765\u63a5\u6536\u548c\u54cd\u5e94\u6570\u636e\u3002<\/p>\n
\u5b9e\u4f8b\u5982\u4e0b\uff0c\u5728\u4f60\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u53eb server.js \u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n
var http = require('http');\r\n\r\nhttp.createServer(function (request, response) {\r\n\r\n\/\/ \u53d1\u9001 HTTP \u5934\u90e8\r\n\/\/ HTTP \u72b6\u6001\u503c: 200 : OK\r\n\/\/ \u5185\u5bb9\u7c7b\u578b: text\/plain\r\nresponse.writeHead(200, {'Content-Type': 'text\/plain'});\r\n\r\n\/\/ \u53d1\u9001\u54cd\u5e94\u6570\u636e \"Hello World\"\r\nresponse.end('Hello World,www.linuxidc.com\\n');\r\n}).listen(8888);\r\n\r\n\/\/ \u7ec8\u7aef\u6253\u5370\u5982\u4e0b\u4fe1\u606f\r\nconsole.log('Server running at http:\/\/127.0.0.1:8888\/');\r\n<\/pre>\n\u4ee5\u4e0a\u4ee3\u7801\u6211\u4eec\u5b8c\u6210\u4e86\u4e00\u4e2a\u53ef\u4ee5\u5de5\u4f5c\u7684 HTTP \u670d\u52a1\u5668\u3002<\/p>\n
\u4f7f\u7528 node \u547d\u4ee4\u6267\u884c\u4ee5\u4e0a\u7684\u4ee3\u7801\uff1a<\/p>\n
node server.js\r\nServer running at http:\/\/127.0.0.1:8888\/\r\n<\/pre>\n\u63a5\u4e0b\u6765\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/127.0.0.1:8888\/\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u5199\u7740 \"Hello World,www.linuxidc.com\"\u7684\u7f51\u9875\u3002<\/p>\n
<\/p>\n
\u5206\u6790Node.js \u7684 HTTP \u670d\u52a1\u5668\uff1a
\n\u7b2c\u4e00\u884c\u8bf7\u6c42\uff08require\uff09Node.js \u81ea\u5e26\u7684 http \u6a21\u5757\uff0c\u5e76\u4e14\u628a\u5b83\u8d4b\u503c\u7ed9 http \u53d8\u91cf\u3002
\n\u63a5\u4e0b\u6765\u6211\u4eec\u8c03\u7528 http \u6a21\u5757\u63d0\u4f9b\u7684\u51fd\u6570\uff1a createServer \u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de \u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6709\u4e00\u4e2a\u53eb\u505a listen \u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u6709\u4e00\u4e2a\u6570\u503c\u53c2\u6570\uff0c \u6307\u5b9a\u8fd9\u4e2a HTTP \u670d\u52a1\u5668\u76d1\u542c\u7684\u7aef\u53e3\u53f7\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/www.linuxidc.com\/Linux\/2018-03\/151520.htm<\/a><\/p>\n\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/node-js-linux.html<\/a>\u7f16\u8f91\uff1a\u6768\u658c\u658c\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"\u5bfc\u8bfb Node.js\u662f\u7528\u4e8e\u6267\u884c\u670d\u52a1\u5668\u7aefJavaScript\u4ee3\u7801\u5e7f\u6cdb\u4f7f\u7528\u7684\u5f00\u6e90\u548c\u8de8\u5e73\u53f0JavaScript\u8fd0\u884c\u65f6 […]<\/p>\n","protected":false},"author":1476,"featured_media":98456,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-98246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/98246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1476"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=98246"}],"version-history":[{"count":9,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/98246\/revisions"}],"predecessor-version":[{"id":98726,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/98246\/revisions\/98726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/98456"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=98246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=98246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=98246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}