﻿{"id":875,"date":"2014-02-06T02:14:03","date_gmt":"2014-02-05T18:14:03","guid":{"rendered":"http:\/\/notes.zerobox.org\/?p=875"},"modified":"2014-02-06T02:14:03","modified_gmt":"2014-02-05T18:14:03","slug":"%e9%80%81%e7%bb%99node-js%e6%96%b0%e6%89%8b%e7%9a%847%e4%b8%aa%e5%bc%80%e5%8f%91%e5%b0%8f%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/875.html","title":{"rendered":"\u9001\u7ed9Node.js\u65b0\u624b\u76847\u4e2a\u5f00\u53d1\u5c0f\u6280\u5de7"},"content":{"rendered":"<p>\u6216\u8005\u5f53\u6211\u5f00\u59cb\u5b66\u4e60\u7684\u65f6\u5019\uff0c\u6211\u5e0c\u671b\u77e5\u9053\u7684\u3002<br \/>\nNode.js\u7684\u5f00\u53d1\u76f8\u5f53\u6709\u8da3\u548c\u4ee4\u4eba\u6109\u5feb\u7684\u3002\u6709\u8d85\u8fc73\u4e075\u5343\u4e2a\u6a21\u5757\u53ef\u4f9b\u9009\u62e9\uff0c\u5e76\u4e14\u66f4\u91cd\u8981\u7684\u662f\u7528node\u5f00\u53d1\u7684\u5f88\u5bb9\u6613\u6269\u5c55\u3002<br \/>\n\u5bf9\u4e8e\u521a\u63a5\u89e6 Node.js \u7684\u5f00\u53d1\u8005\uff0c\u514d\u4e0d\u4e86\u4e00\u4e9b\u78d5\u78d5\u78b0\u78b0\u3002\u5728\u8fd9\u7bc7\u7b80\u77ed\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u63d0\u5230\u4e86\u4e00\u4e9b\u6211\u5b66\u4e60 Node.js \u65f6\u9047\u5230\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6280\u5de7 1 \uff1a\u5f00\u53d1\u73af\u5883\u4f7f\u7528 nodemon\uff0c\u751f\u4ea7\u73af\u5883\u4f7f\u7528 pm2<br \/>\n\u5f53\u4f60\u7740\u624b Node.js \u5f00\u53d1\u65f6\uff0c\u975e\u5e38\u5c34\u5c2c\u7684\u662f\uff0c\u5fc5\u987b\u4e00\u904d\u53c8\u4e00\u904d\u7684\u8fd0\u884c node [file].js \u3002\u5728\u6211\u5f00\u59cb\u5b66 node \u65f6\uff0c\u8fd9\u70b9\u76f8\u5f53\u4ee4\u6211\u5f88\u4e0d\u8010\u70e6\u3002\u7279\u522b\u662f\u6bcf\u6b21\u4fee\u6539\u4e00\u70b9\u4e1c\u897f\uff0c\u90fd\u8981 control + C\u4e00\u4e0b \u3002\u5e78\u8fd0\u7684\u662f\u6211\u53d1\u73b0\u4e86\u4e00\u4e2a\u975e\u5e38\u68d2\u7684\u5de5\u5177 Nodemon \u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\uff1anpm install -g nodemon<br \/>\nNodemon \u662f\u4e2a\u5f88\u597d\u7684\u5de5\u5177\uff0c\u4e00\u65e6\u5168\u5c40\u5b89\u88c5\u4e86\u5b83\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 nodemon [file].js \u8fd0\u884c node.js \u811a\u672c\u3002nodemon \u4f1a\u76d1\u63a7\u4f60\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u6240\u6709\u4f9d\u8d56\u4ee3\u7801\u7684\u53d8\u5316\u3002\u5982\u6b64\u5f00\u53d1 Node.js \uff0c\u5f00\u53d1\u6548\u7387\u77ac\u95f4\u63d0\u9ad8\u3002<br \/>\n\u751f\u4ea7\u73af\u5883\u5462\uff1f\u9664\u975e\u4f60\u7528\u4e86 Heroku\uff0cNodejitsu \u6216\u8005\u5176\u5b83\u4e0d\u9519\u7684 Node.js \u6258\u7ba1\u670d\u52a1\uff0c\u5426\u5219\u4f60\u53ef\u4ee5\u8bd5\u8bd5 EC2 \u6216\u8005\u5176\u5b83\u7684\u4e91\u670d\u52a1\uff0c\u901a\u8fc7\u5b83\u4eec\u8fd0\u884c\u4f60\u7684 Node.js \u5e94\u7528\u3002\u5982\u4f55\u4fdd\u8bc1 Node.js \u5e94\u7528\u4e00\u76f4\u8fd0\u884c\u5462\uff1fPM2 \u50cf nodemon \uff0c\u7528\u4e8e\u5728\u751f\u4ea7\u73af\u5883\u8fd0\u884c node \u5e94\u7528\u3002\u5b83\u4f1a\u76d1\u63a7\u4f60\u7684 app \u53d8\u5316\u5e76\u91cd\u65b0\u90e8\u7f72\u5b83\u4eec\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5982\u679c PM2 \u9047\u5230\u4e8b\u6545\uff0c\u5b83\u5c06\u7acb\u523b\u91cd\u542f\u4f60\u7684 node.js \u5e94\u7528\u3002<br \/>\n\u5f53\u4f60\u7684\u5e94\u7528\u9700\u8981\u591a\u6838\u5904\u7406\u7684\u65f6\u5019\uff0c\u51f8\u663e\u51fa\u4e86 PM2 \u7684\u4f18\u52bf\u3002PM2 \u5185\u90e8\u96c6\u6210\u7684\u201c\u8d1f\u8f7d\u5747\u8861\u201d\u8ba9\u4f60\u8f7b\u677e\u6307\u5b9a Node \u5e94\u7528\u8fd0\u884c\u51e0\u4e2a\u5b9e\u4f8b\u3002<br \/>\n1<br \/>\npm2 start app.js -i max<br \/>\n-i \u53c2\u6570\u7528\u6765\u6307\u5b9a\u8fd0\u884c\u591a\u5c11\u4e2a\u5b9e\u4f8b\uff0c\u6b64\u4f8b\u4e2d PM2 \u4f7f\u7528\u4e86\u4e00\u4e2a\u5e38\u91cf max \u81ea\u52a8\u6269\u5c55\u4f60\u7684 app \u8fd0\u8f6c\u5230\u6700\u5927\u6838\u6570\uff0c\u4e0d\u8981\u5fd8\u8bb0 Node \u5e73\u65f6\u53ea\u8fd0\u884c\u5728\u5355\u6838\u4e0a\uff01<\/p>\n<p>\u6280\u5de7 2 \uff1aAsync \u6216\u8005 Q<br \/>\n\u8d8a\u65e9\u5199 node.js \u5e94\u7528\uff0c\u8d8a\u4f1a\u5c3d\u65e9\u9886\u7565\u56de\u8c03\u4e4b\u75db\u3002\u8fd9\u6709\u4e00\u4e2a\u4f8b\u5b50\uff1a<br \/>\n<code><br \/>\nfunction register(name, password, cb){<br \/>\n checkIfNameExists(name, function(err, result){<br \/>\n if(err){<br \/>\n return cb(\u201cerror\u201d);<br \/>\n }<br \/>\n checkIfPasswordGood(password, function(err, result){<br \/>\n if(err){<br \/>\n return cb(\u201cerror\u201d);<br \/>\n }<br \/>\ncreateAccount(name,password, function(err,result){<br \/>\n if(err){<br \/>\n return cb(\u201cerror\u201d);<br \/>\n }<br \/>\n createBlog(name, function(err, result){<br \/>\n sendEmail(name, function(err, result){<br \/>\n callback(result);<br \/>\n });<br \/>\n });<br \/>\n });<br \/>\n });<br \/>\n });<br \/>\n }<\/code><br \/>\n\u4e0d\u600e\u4e48\u7f8e\u7684\u4ee3\u7801\u7684\u786e\u5b58\u5728\uff0c\u4f46\u4f60\u5982\u4f55\u907f\u514d\u5462\uff1f<br \/>\n\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u7528 events \uff0c\u6211\u4e2a\u4eba\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a\uff0c\u5b83\u8fdd\u80cc\u4e86\u4e00\u4e2a\u51fd\u6570\u7684\u89c2\u70b9\uff0c\u53ef\u4ee5\u7528 events \u8c03\u7528\u53ea\u6709\u4e00\u4e2a\u7528\u9014\u7684\u79c1\u6709\u65b9\u6cd5\u3002<br \/>\n\u6240\u4ee5\u600e\u4e48\u505a\u5462\uff1f\u6709\u4e24\u4e2a\u76f8\u4e92\u7ade\u4e89\u7684\u5e93\uff1aasync.js \u548c Q \uff0c\u5b83\u4eec\u5747\u53ef\u4ee5\u907f\u514d\u56de\u8c03\u51fa\u73b0\u3002<br \/>\nAsync.js \u6216\u8005 \u201casync\u201d\u53ef\u4ee5\u8f7b\u677e\u6267\u884c\u8fde\u7eed\u6216\u8005\u5e73\u884c\u7684\u51fd\u6570\uff0c\u4e0d\u9700\u8981\u4e00\u5c42\u4e00\u5c42\u7684\u5d4c\u5957\u3002<br \/>\n\u4e0b\u9762\u662f Async \u652f\u6301\u7684\u6a21\u5f0f\uff0creadme \u6709\u8bb0\u5f55\u3002\u60f3\u4e86\u89e3 async \u652f\u6301\u7684\u6240\u6709\u6a21\u5f0f\uff0c\u770b\u770b\u5b83\u4eec\u7684\u4ee3\u7801\u4ed3\u5e93\u3002<br \/>\n<code><br \/>\nasync.map([\u2018file1',\u2019file2',\u2019file3'], fs.stat, function(err, results){<br \/>\n \/\/ results is now an array of stats for each file<br \/>\n });<br \/>\nasync.filter([\u2018file1',\u2019file2',\u2019file3'], fs.exists, function(results){<br \/>\n \/\/ results now equals an array of the existing files<br \/>\n });<br \/>\nasync.parallel([<br \/>\n function(){ \u2026 },<br \/>\n function(){ \u2026 }<br \/>\n ], callback);<br \/>\nasync.series([<br \/>\n function(){ \u2026 },<br \/>\n function(){ \u2026 }<br \/>\n ]);<br \/>\nasync.waterfall([<br \/>\n function(callback){<br \/>\n callback(null, \u2018one\u2019, \u2018two\u2019);<br \/>\n },<br \/>\n function(arg1, arg2, callback){<br \/>\n callback(null, \u2018three\u2019);<br \/>\n },<br \/>\n function(arg1, callback){<br \/>\n \/\/ arg1 now equals \u2018three\u2019<br \/>\n callback(null, \u2018done\u2019);<br \/>\n }<br \/>\n ], function (err, result) {<br \/>\n \/\/ result now equals \u2018done\u2019<br \/>\n });<br \/>\n<\/code><br \/>\n\u5982\u679c\u6211\u4eec\u7528 async \u7684 waterfall \u6765\u4fee\u6539\u4e4b\u524d\u7684\u4f8b\u5b50\uff0c\u4ee3\u7801\u5c06\u66f4\u52a0\u6613\u8bfb\uff0c\u518d\u4e5f\u4e0d\u4f1a\u6d89\u53ca\u6b7b\u4ea1\u91d1\u5b57\u5854\u4e86\u3002<br \/>\n\u53e6\u4e00\u4e2a\u5f88\u597d\u7684\u5e93\u662f Q \uff0c\u8fd9\u4e2a\u5e93\u7528\u5230\u4e86 promises \u7684\u6982\u5ff5\u3002Promise \u662f\u4e00\u4e2a\u542b\u6709\u2018promise\u2019\u65b9\u6cd5\u7684\u8fd4\u56de\u5bf9\u8c61\uff0c\u4ed6\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6700\u7ec8\u8fd4\u56de\u503c\uff0c\u975e\u5e38\u4f18\u96c5\u7684\u5c06 javascripts \u7684\u5f02\u6b65\u7279\u6027\u548c node.js \u7d27\u5bc6\u8054\u7cfb\u8d77\u6765\u3002<br \/>\n\u4ece Q \u7684\u4ee3\u7801\u4ed3\u5e93\u4e2d\u62ff\u6765\u7684\u4f8b\u5b50\u3002<br \/>\n<code><br \/>\npromiseMeSomething()<br \/>\n .then(function (value) {<br \/>\n }, function (reason) {<br \/>\n });<br \/>\n<\/code><br \/>\npromise me \u51fd\u6570\u7acb\u523b\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8c03\u7528 then \u5c06\u8fd4\u56de\u4f20\u5165 value \u503c\u7684\u51fd\u6570\u3002\u5b83\u4e5f\u5e26\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5904\u7406\u672a\u80fd\u8fd4\u56de\u503c\u7684\u60c5\u51b5\u3002<br \/>\n\u7528\u975e\u5e38\u6574\u6d01\u7684\u65b9\u5f0f\u907f\u514d\u4e86\u56de\u8c03\u3002\u5982\u679c\u91cd\u5199\u539f\u6765\u7684\u4f8b\u5b50\uff0c\u5f53 then \u6267\u884c\u7684\u65f6\u5019\u624d\u8c03\u7528\u6bcf\u4e2a\u51fd\u6570\u3002<\/p>\n<p><code>Q.fcall(checkIfNameExists)<br \/>\n .then(checkIfPasswordIsGood)<br \/>\n .then(createAccount)<br \/>\n .then(createBlog)<br \/>\n .then(function (result) {<br \/>\n \/\/ Do something with the result<br \/>\n })<br \/>\n .catch(function (error) {<br \/>\n \/\/ Handle any error from all above steps<br \/>\n })<br \/>\n .done();<\/code><br \/>\n\u5982\u524d\u9762\u6240\u8bf4\uff0c\u6211\u4e0d\u559c\u6b22\u521b\u5efa\u5355\u4e00\u76ee\u6807\u51fd\u6570\u3002\u53d6\u800c\u4ee3\u4e4b\uff0c\u7528 \u201cthen\u201d\u4f20\u9012\u51fd\u6570\uff0c\u4ec5\u4f20\u9012\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5f53\u7136\u9009\u62e9\u5668\u5728\u4f60\u624b\u4e2d\u3002<br \/>\n\u603b\u4e4b\uff0c\u5f53\u4f60\u6389\u5165\u56de\u8c03\u5730\u72f1\u65f6\uff0c\u662f\u8be5\u5173\u6ce8\u4e0b async.js \u6216\u8005 Q \u4e86\u3002<br \/>\n\u201c\u6211\u4e2a\u4eba\u559c\u597d\uff1f\u4e00\u76f4\u662f Q\uff01\u201d<\/p>\n<p>\u6280\u5de7 3 \uff1a\u8f7b\u677e\u8c03\u8bd5 Node.js \u5e94\u7528<br \/>\n\u5982\u679c\u4f60\u4ece\u4e00\u4e2a IDE \u91cd\u5ea6\u96c6\u6210\u7684\u8bed\u8a00\u6bd4\u5982 java \u6216\u8005 C# \u8f6c\u6765\u8c03\u8bd5 Node.js\uff0c\u4f60\u4e00\u5b9a\u4f1a\u611f\u5230\u5f88\u56f0\u6270\u3002\u5927\u591a\u6570\u65b0\u7684 node \u5f00\u53d1\u8005\u91c7\u7528 \u201cflow\u201d\u8c03\u8bd5\u6a21\u5f0f\uff0c\u4f60\u6700\u597d\u7684\u5e2e\u624b\u662f console.log \u3002<br \/>\n\u4f46\u662f\u80af\u5b9a\u6709\u66f4\u4fbf\u5229\u7684\u8c03\u8bd5\u65b9\u5f0f\uff0cNode.js \u5185\u7f6e\u4e86\u4e00\u4e2a\u8c03\u8bd5\u5668\u4f60\u53ef\u4ee5\u53eb\u5b83 node debug\uff0c\u4e0d\u8fc7\u6211\u66f4\u559c\u6b22\u7684 node-inspector \u3002<br \/>\n\u5b83\u4eec\u7684 github \u4ed3\u5e93\u63d0\u5230\u201cNode Inspector \u662f\u4f7f\u7528\u4e86 Blink \u5f00\u53d1\u5de5\u5177\u7684 node.js \u8c03\u8bd5\u5de5\u5177\u754c\u9762\uff08\u5148\u524d\u7684 WebKit Web Inspector\uff09\u3002\u201d<br \/>\n\u7b80\u800c\u8a00\u4e4b\uff0c\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u4e2a\u7f16\u8f91\u5668\u548c chrome web tools \uff0cnode-inspector \u90fd\u53ef\u4ee5\u8c03\u8bd5\u4f60\u7684\u5e94\u7528\u3002Node Inspector \u53ef\u4ee5\u505a\u4e00\u4e9b\u771f\u6b63\u9177\u7684\u4e1c\u897f\uff0c\u6bd4\u5982\u5b9e\u65f6\u4ee3\u7801\u4fee\u6539\uff0c\u5355\u6b65\u8c03\u8bd5\uff0c\u4f5c\u7528\u57df\u6ce8\u5165\u548c\u4e00\u5806\u5f88\u9177\u7684\u529f\u80fd\u3002<br \/>\n\u5b83\u6d89\u53ca\u5230\u4e00\u4e9b\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u6309\u7167\u8fd9\u91cc https:\/\/github.com\/node-inspector\/node-inspector \u7684\u8bf4\u660e\u505a\u3002<\/p>\n<p>\u6280\u5de7 4 \uff1aNodefly<br \/>\n\u4e00\u65e6\u4f60\u7684\u5e94\u7528\u6b63\u5e38\u8fd0\u884c\uff0c\u4f60\u53ef\u80fd\u4f1a\u95ee\u81ea\u5df1\uff0c\u5982\u4f55\u76d1\u63a7\u5b83\u7684\u6027\u80fd\uff0c\u5982\u4f55\u901a\u8fc7\u5206\u6790\u786e\u4fdd\u5b83\u4ee5\u6700\u4f73\u901f\u5ea6\u8fd0\u884c\u5462\u3002\u6700\u7b80\u5355\u7684\u56de\u7b54\u662f\u4f7f\u7528\u975e\u5e38\u68d2\u7684 Nodefly \u670d\u52a1\u3002<br \/>\n\u901a\u8fc7\u4e00\u884c\u7b80\u5355\u7684\u4ee3\u7801\uff0cNodefly \u5f00\u59cb\u76d1\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u5185\u5b58\u6cc4\u9732\uff0c \u6d4b\u91cf redis \u7528\u4e86\u591a\u4e45\uff0cmongo \u67e5\u8be2\u548c\u4e00\u5806\u5176\u4ed6\u5f88\u9177\u7684\u4e1c\u897f\u3002<br \/>\nhttp:\/\/www.nodefly.com\/<\/p>\n<p>\u6280\u5de7 5 \uff1a\u7528 NPM \u7ba1\u7406\u6a21\u5757<br \/>\nnode \u6700\u5bfb\u5e38\u7684\u4e8b\u60c5\u4e4b\u4e00\u662f\u901a\u8fc7 NPM \u5b89\u88c5\u7a0b\u5e8f\u5305\u3002Node \u6709\u4e2a\u795e\u5947\u7684\u5305\u7ba1\u7406\u5668\uff0c\u5b83\u4f1a\u5b89\u88c5 package.json \u6e05\u5355\u6587\u4ef6\u91cc\u6307\u5b9a\u7684\u6a21\u5757\u3002\u53ef\u662f\u521d\u5b66\u8005\u90fd\u4f1a\u9047\u5230\u4e00\u4ef6\u4e8b\u60c5\uff0cpackage.json \u6587\u4ef6\u4e2d\u6240\u6709\u4f7f\u7528\u7684\u6a21\u5757\u5982\u4f55\u4fdd\u6301\u6700\u65b0\u3002<br \/>\n\u603b\u662f\u6253\u5f00 package.json \u66f4\u65b0\u5df2\u5b89\u88c5\u6a21\u5757\u7684\u4f9d\u8d56\u5c5e\u6027\u4f3c\u4e4e\u5f88\u75db\u82e6\uff0c\u5f88\u591a\u4eba\u4e0d\u77e5\u9053 npm \u53ef\u4ee5\u5e2e\u4f60\u505a\u8fd9\u4e9b\uff01<br \/>\n\u7b80\u5355\u8fd0\u884c npm install\u200a\u2014\u200asave module_name \uff0cnpm \u5c06\u81ea\u52a8\u66f4\u65b0\u5e26\u6709\u6b63\u786e\u6a21\u5757\u548c\u7248\u672c\u53f7\u7684 package.json<br \/>\nnpm install \u2014save module_name<\/p>\n<p>\u6280\u5de7 6 \uff1a\u4e0d\u8981\u63d0\u4ea4 node_modules \u6587\u4ef6\u5939<br \/>\n\u6211\u4eec\u4e00\u76f4\u8c08\u8bba\u6a21\u5757\u548c npm \uff0c\u8fd8\u6709\u4eba\u4e0d\u77e5\u9053\u4e0d\u8be5\u63d0\u4ea4 node_modules \u6587\u4ef6\u5939\uff0c\u6700\u5927\u7684\u539f\u56e0\u662f\u6ca1\u6709\u5fc5\u8981\u63d0\u4ea4\u3002\u5f53\u522b\u4eba\u68c0\u51fa\u4f60\u7684\u4ee3\u7801\uff0c\u4ed6\u4eec\u53ef\u4ee5\u7528 npm \u5b89\u88c5\u6240\u9700\u6a21\u5757\u3002<br \/>\n\u4f60\u4e5f\u8bb8\u4f1a\u8bf4\u63d0\u4ea4 node_modules \u6587\u4ef6\u5939\u65e0\u4f24\u5927\u96c5\uff0c\u4f46\u662f\u5982\u679c\u68c0\u51fa\u4f60\u4ee3\u7801\u7684\u4eba\u7528\u8ddf\u4f60\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u5462\uff0c\u901a\u8fc7 npm \u5b89\u88c5\u7684\u6a21\u5757\u662f\u7f16\u8bd1\u8fc7\u7684\uff1f\u4f60\u7684\u5e94\u7528\u4f1a\u5d29\u6e83\uff0c\u68c0\u51fa\u4f60\u4ee3\u7801\u7684\u4eba\u5b8c\u5168\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\uff01<br \/>\n\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5f53\u4f60\u5b89\u88c5\u8fc7 bcrypt \u548c sentimental \u6a21\u5757\u540e\uff0c\u5b83\u4eec\u5728\u4e3b\u673a\u4e0a\u662f\u7f16\u8bd1\u8fc7\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u539f\u59cb\u7ec4\u4ef6\u662f\u7528 C \u5199\u7684\u3002<br \/>\n\u6700\u4f18\u7684\u4f5c\u6cd5\u662f\u628a node_modules \u6587\u4ef6\u5939\u52a0\u5230 .gitignore \u91cc\uff0c\u907f\u514d\u63d0\u4ea4\u5b83\u3002<br \/>\n\/\/ .gitignore node_modules\/*<\/p>\n<p>\u6280\u5de7 7 \uff1a\u4e0d\u8981\u5fd8\u8bb0 return<br \/>\n\u521a\u5165\u95e8\u7684 node \u5f00\u53d1\u8005\u901a\u5e38\u5bb9\u6613\u72af\u4e2a\u9519\u8bef\uff0ccallback \u56de\u8c03\u51fd\u6570\u540e\u9762\u5fd8\u52a0 return \u3002\u867d\u7136\u6709\u65f6\u5019\u6ca1\u4ec0\u4e48\u5f71\u54cd\uff0c\u591a\u6570\u60c5\u51b5\u4f60\u4f1a\u9047\u5230\u5947\u602a\u7684\u95ee\u9898\uff0ccallback \u56de\u8c03\u6267\u884c\u4e86\u4e24\u6b21\u3002<br \/>\n\u770b\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<p><code>function do(err,result, callback){<br \/>\n if(err){<br \/>\n callback(\u201cerror\u201d);<br \/>\n }<br \/>\n callback(\u201cgood\u201d);<br \/>\n }<\/code><br \/>\n\u4e4d\u4e00\u770b\uff0c\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u5982\u679c\u6709\u9519\u8bef\uff0c\u628a \u201cerror\u201d\u4f20\u5165 callback\uff0c\u6ca1\u9519\u5219\u4f20\u9012\u201cgood\u201d\u3002\u4f46\u662f\u8c03\u7528 callback \u4ee5\u540e\u5e76\u6ca1\u6709\u505c\u6b62\u6267\u884c\uff0c\u4f1a\u63a5\u7740\u8c03\u7528 callback(\u201cgood\u201d) \u3002\u5728\u590d\u6742\u7684\u4ee3\u7801\u4e2d\uff0c\u52a0\u4e0a return \u4f1a\u8282\u7ea6\u6570\u5c0f\u65f6\u7684\u8c03\u8bd5\u65f6\u95f4\u3002<\/p>\n<p>Node.js \u662f\u4e2a\u5f88\u8d5e\u7684\u5f00\u53d1\u5e73\u53f0\uff0c\u5982\u679c\u4f60\u8c28\u8bb0\u8fd9 7 \u6761\u6280\u5de7\uff0c\u5f00\u53d1\uff0c\u8c03\u8bd5\u548c\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u65f6\uff0c\u53ef\u4ee5\u8282\u7701\u4e0d\u5c11\u65f6\u95f4\uff0c\u9632\u6b62\u5934\u53d1\u5c3d\u65e9\u53d8\u7070\u767d\u3002<\/p>\n<p>\u539f\u6587\uff1ahttps:\/\/medium.com\/tech-talk\/7-tips-for-a-node-dot-js-padawan\/ by Faisal Abid<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6216\u8005\u5f53\u6211\u5f00\u59cb\u5b66\u4e60\u7684\u65f6\u5019\uff0c\u6211\u5e0c\u671b\u77e5\u9053\u7684\u3002 Node.js\u7684\u5f00\u53d1\u76f8\u5f53\u6709\u8da3\u548c\u4ee4\u4eba\u6109\u5feb\u7684 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/875.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[219],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","tag-node-js"],"views":600,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=875"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/875\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}