{"id":1210,"date":"2013-05-24T10:33:08","date_gmt":"2013-05-24T02:33:08","guid":{"rendered":"http:\/\/www.strongd.net\/?p=1210"},"modified":"2013-05-24T10:33:08","modified_gmt":"2013-05-24T02:33:08","slug":"node-js-%e7%a9%b6%e7%ab%9f%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1210","title":{"rendered":"Node.js \u7a76\u7adf\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p>Node \u662f\u4e00\u4e2a\u670d\u52a1\u5668\u7aef JavaScript \u89e3\u91ca\u5668\uff0c\u5b83\u5c06\u6539\u53d8\u670d\u52a1\u5668\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u7684\u6982\u5ff5\u3002\u5b83\u7684\u76ee\u6807\u662f\u5e2e\u52a9\u7a0b\u5e8f\u5458\u6784\u5efa\u9ad8\u5ea6\u53ef\u4f38\u7f29\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u7f16\u5199\u80fd\u591f\u5904\u7406\u6570\u4e07\u6761\u540c\u65f6\u8fde\u63a5\u5230\u4e00\u4e2a\uff08\u53ea\u6709\u4e00\u4e2a\uff09\u7269\u7406\u673a\u7684\u8fde\u63a5\u4ee3\u7801\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6e05\u5355 2. Node \u968f\u673a\u6570\u5b57\u751f\u6210\u5668<\/strong><br \/>\n\/\/ these modules need to be imported in order to use them.<br \/>\n\/\/ Node has several modules. They are like any #include<br \/>\n\/\/ or import statement in other languages<br \/>\nvar http = require(&#8220;http&#8221;);<br \/>\nvar url = require(&#8220;url&#8221;);<\/p>\n<p>\/\/ The most important line in any Node file. This function<br \/>\n\/\/ does the actual process of creating the server. Technically,<br \/>\n\/\/ Node tells the underlying operating system that whenever a<br \/>\n\/\/ connection is made, this particular callback function should be<br \/>\n\/\/ executed. Since we&#8217;re creating a web service with REST API,<br \/>\n\/\/ we want an HTTP server, which requires the http variable<br \/>\n\/\/ we created in the lines above.<br \/>\n\/\/ Finally, you can see that the callback method receives a &#8216;request&#8217;<br \/>\n\/\/ and &#8216;response&#8217; object automatically. This should be familiar<br \/>\n\/\/ to any PHP or Java programmer.<br \/>\nhttp.createServer(function(request, response) {<\/p>\n<p>\/\/ The response needs to handle all the headers, and the return codes<br \/>\n\/\/ These types of things are handled automatically in server programs<br \/>\n\/\/ like Apache and Tomcat, but Node requires everything to be done yourself<br \/>\nresponse.writeHead(200, {&#8220;Content-Type&#8221;: &#8220;text\/plain&#8221;});<\/p>\n<p>\/\/ Here is some unique-looking code. This is how Node retrives<br \/>\n\/\/ parameters passed in from client requests. The url module<br \/>\n\/\/ handles all these functions. The parse function<br \/>\n\/\/ deconstructs the URL, and places the query key-values in the<br \/>\n\/\/ query object. We can find the value for the &#8220;number&#8221; key<br \/>\n\/\/ by referencing it directly &#8211; the beauty of JavaScript.<br \/>\nvar params = url.parse(request.url, true).query;<br \/>\nvar input = params.number;<\/p>\n<p>\/\/ These are the generic JavaScript methods that will create<br \/>\n\/\/ our random number that gets passed back to the caller<br \/>\nvar numInput = new Number(input);<br \/>\nvar numOutput = new Number(Math.random() * numInput).toFixed(0);<\/p>\n<p>\/\/ Write the random number to response<br \/>\nresponse.write(numOutput);<\/p>\n<p>\/\/ Node requires us to explicitly end this connection. This is because<br \/>\n\/\/ Node allows you to keep a connection open and pass data back and forth,<br \/>\n\/\/ though that advanced topic isn&#8217;t discussed in this article.<br \/>\nresponse.end();<\/p>\n<p>\/\/ When we create the server, we have to explicitly connect the HTTP server to<br \/>\n\/\/ a port. Standard HTTP port is 80, so we&#8217;ll connect it to that one.<br \/>\n}).listen(80);<\/p>\n<p>\/\/ Output a String to the console once the server starts up, letting us know everything<br \/>\n\/\/ starts up correctly<br \/>\nconsole.log(&#8220;Random Number Generator Running&#8230;&#8221;);<\/p>\n<p>\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<br \/>\n\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u653e\u5165\u4e00\u4e2a\u540d\u4e3a \u201crandom.js\u201d \u7684\u6587\u4ef6\u4e2d\u3002\u73b0\u5728\uff0c\u8981\u542f\u52a8\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5e76\u8fd0\u884c\u5b83\uff08\u4ee5\u4fbf\u521b\u5efa HTTP \u670d\u52a1\u5668\u5e76\u76d1\u542c\u7aef\u53e3 80 \u4e0a\u7684\u8fde\u63a5\uff09\uff0c\u53ea\u9700\u5728\u60a8\u7684\u547d\u4ee4\u63d0\u793a\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a% node random.js\u3002\u4e0b\u9762\u662f\u670d\u52a1\u5668\u5df2\u7ecf\u542f\u52a8\u5e76\u8fd0\u884c\u65f6\u770b\u8d77\u6765\u7684\u6837\u5b50\uff1a<br \/>\nroot@ubuntu:\/home\/moila\/ws\/mike# node random.js<br \/>\nRandom Number Generator Running&#8230;<\/p>\n<p>\u8bbf\u95ee\u5e94\u7528\u7a0b\u5e8f<br \/>\n\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u542f\u52a8\u5e76\u8fd0\u884c\u3002Node \u6b63\u5728\u76d1\u542c\u6240\u6709\u8fde\u63a5\uff0c\u6211\u4eec\u6765\u6d4b\u8bd5\u4e00\u4e0b\u3002\u7531\u4e8e\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 RESTful API\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 Web \u6d4f\u89c8\u5668\u6765\u8bbf\u95ee\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u952e\u5165\u4ee5\u4e0b\u5730\u5740\uff08\u786e\u4fdd\u60a8\u5df2\u5b8c\u6210\u4e86\u4e0a\u9762\u7684\u6b65\u9aa4\uff09\uff1ahttp:\/\/localhost\/?number=27\u3002<br \/>\n\u60a8\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u5c06\u66f4\u6539\u5230\u4e00\u4e2a\u4ecb\u4e8e 0 \u5230 27 \u4e4b\u95f4\u7684\u968f\u673a\u6570\u5b57\u3002\u5355\u51fb\u6d4f\u89c8\u5668\u4e0a\u7684 \u201c\u91cd\u65b0\u8f7d\u5165\u201d \u6309\u94ae\uff0c\u60a8\u4f1a\u5f97\u5230\u53e6\u4e00\u4e2a\u968f\u673a\u6570\u5b57\u3002\u5c31\u662f\u8fd9\u6837\uff0c\u8fd9\u5c31\u662f\u60a8\u7684\u7b2c\u4e00\u4e2a Node \u5e94\u7528\u7a0b\u5e8f\uff01<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5b66\u4e60<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.nodejs.org\/\">Node.js \u4e3b\u9875<\/a>\u00a0\u662f\u4e86\u89e3\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u5207\u5165\u70b9\u3002<\/li>\n<li><a href=\"http:\/\/www.nodejs.org\/#download\">\u5728\u8fd9\u91cc\u4e0b\u8f7d Node.js<\/a>\u3002\u60a8\u8fd8\u5c06\u9700\u8981\u00a0<a href=\"http:\/\/www.python.org\/\">Python<\/a>\u3002<\/li>\n<li>\u6d4f\u89c8\u00a0<a href=\"http:\/\/www.nodejs.org\/api.html\">Node.js API \u9875<\/a>\u3002\u6ce8\u610f\uff0c\u4e0d\u540c\u53d1\u5e03\u7684\u8bed\u6cd5\u53ef\u80fd\u4e0d\u540c\uff0c\u56e0\u6b64\uff0c\u8bf7\u4ed4\u7ec6\u68c0\u67e5\u60a8\u5df2\u4e0b\u8f7d\u7684\u7248\u672c\u548c\u6b63\u5728\u6d4f\u89c8\u7684 API\u3002<\/li>\n<li>\u53c2\u89c1\u00a0<a href=\"https:\/\/github.com\/joyent\/node\/wiki\/modules\">Node \u6a21\u5757\u9875<\/a>\uff0c\u8be5\u9875\u5217\u51fa\u4e86\u6240\u6709\u53ef\u7528\u4e8e Node \u4e2d\u7684\u6a21\u5757\u3002<\/li>\n<li>\u641c\u7d22\u00a0<a href=\"http:\/\/search.npmjs.org\/\">NPM<\/a>\uff0c\u8f7b\u677e\u6269\u5c55\u60a8\u7684 Node \u5b89\u88c5\u7684\u529f\u80fd\u3002<\/li>\n<li>\u968f\u65f6\u5173\u6ce8 developerWorks\u00a0<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/offers\/techbriefings\/\">\u6280\u672f\u6d3b\u52a8<\/a>\u548c<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/swi\/\">\u7f51\u7edc\u5e7f\u64ad<\/a>\u3002<\/li>\n<li>\u8bbf\u95ee developerWorks\u00a0<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/opensource\/\">Open source \u4e13\u533a<\/a>\u83b7\u5f97\u4e30\u5bcc\u7684 how-to \u4fe1\u606f\u3001\u5de5\u5177\u548c\u9879\u76ee\u66f4\u65b0\u4ee5\u53ca<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/opensource\/best2009\/index.html\">\u6700\u53d7\u6b22\u8fce\u7684\u6587\u7ae0\u548c\u6559\u7a0b<\/a>\uff0c\u5e2e\u52a9\u60a8\u7528\u5f00\u653e\u6e90\u7801\u6280\u672f\u8fdb\u884c\u5f00\u53d1\uff0c\u5e76\u5c06\u5b83\u4eec\u4e0e IBM \u4ea7\u54c1\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n<li>\u67e5\u770b\u514d\u8d39\u7684\u00a0<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/offers\/lp\/demos\/\">developerWorks \u6f14\u793a\u4e2d\u5fc3<\/a>\uff0c\u89c2\u770b\u5e76\u4e86\u89e3 IBM \u53ca\u5f00\u6e90\u6280\u672f\u548c\u4ea7\u54c1\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u83b7\u5f97\u4ea7\u54c1\u548c\u6280\u672f<\/strong><\/p>\n<ul>\n<li>\u53c2\u89c1 Wikipedia \u4e0a\u4e0d\u65ad\u53d8\u5316\u7684\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/List_of_free_and_open_source_software_packages\">\u5f00\u6e90\u8f6f\u4ef6\u5305\u5217\u8868<\/a>\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/downloads\/\">IBM \u4ea7\u54c1\u8bc4\u4f30\u8bd5\u7528\u7248\u8f6f\u4ef6<\/a>\u00a0\u6539\u8fdb\u60a8\u7684\u4e0b\u4e00\u4e2a\u5f00\u6e90\u5f00\u53d1\u9879\u76ee\uff0c\u8fd9\u4e9b\u8f6f\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8f7d\u83b7\u5f97\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Node \u662f\u4e00\u4e2a\u670d\u52a1\u5668\u7aef JavaScript \u89e3\u91ca\u5668\uff0c\u5b83\u5c06\u6539\u53d8\u670d\u52a1\u5668\u5e94\u8be5\u5982\u4f55\u5de5\u4f5c\u7684\u6982\u5ff5\u3002\u5b83\u7684\u76ee\u6807\u662f\u5e2e\u52a9\u7a0b\u5e8f\u5458\u6784\u5efa\u9ad8\u5ea6\u53ef\u4f38\u7f29\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u7f16\u5199\u80fd\u591f\u5904\u7406\u6570\u4e07\u6761\u540c\u65f6\u8fde\u63a5\u5230\u4e00\u4e2a\uff08\u53ea\u6709\u4e00\u4e2a\uff09\u7269\u7406\u673a\u7684\u8fde\u63a5\u4ee3\u7801\u3002 &nbsp; \u6e05\u5355 2. Node \u968f\u673a\u6570\u5b57\u751f\u6210\u5668 \/\/ these modules need to be imported in order to use them. \/\/ Node has several modules. They are like any #include \/\/ or import statement in other languages var http = require(&#8220;http&#8221;); var url = require(&#8220;url&#8221;); \/\/ The most important line in any Node file. &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1210\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Node.js \u7a76\u7adf\u662f\u4ec0\u4e48\uff1f<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[194],"class_list":["post-1210","post","type-post","status-publish","format-standard","hentry","category-linux","tag-node-js"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1210"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1210\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1210\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}