{"id":10021186,"date":"2026-01-11T10:57:51","date_gmt":"2026-01-11T02:57:51","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021186"},"modified":"2026-01-11T10:57:51","modified_gmt":"2026-01-11T02:57:51","slug":"%e5%91%8a%e5%88%ab%e9%bb%91%e7%9b%92%ef%bc%81%e6%89%8b%e5%86%99windows%e7%89%88%e7%ae%80%e6%98%93nodemon%ef%bc%8c%e5%ad%a6%e4%b9%a0%e6%96%87%e4%bb%b6%e7%9b%91%e5%90%ac%e4%bb%a3%e7%a0%81%e4%bf%ae","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021186","title":{"rendered":"\u544a\u522b\u9ed1\u76d2\uff01\u624b\u5199Windows\u7248\u7b80\u6613NodeMON\uff0c\u5b66\u4e60\u6587\u4ef6\u76d1\u542c\u4ee3\u7801\u4fee\u6539\u4e0e\u8fdb\u7a0b\u670d\u52a1\u91cd\u542f\u77e5\u8bc6"},"content":{"rendered":"<hr>\n<h2>\u4e3a\u4ec0\u4e48\u8981\u624b\u5199 NodeMON\uff1f<\/h2>\n<p>\u5728\u5f00\u53d1 Node.js \u5e94\u7528\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u7528 <a href=\"https:\/\/link.segmentfault.com\/?enc=HE7tj0uePpJAG2CwWkQ9wA%3D%3D.x9B%2F0BW3%2FSV%2BrAS7T4kvtCNiorExnLysAw7nNhlAB8tvTKVy0omAh9sI3YRbFlnd\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"><code>nodemon<\/code><\/a> \u6765\u81ea\u52a8\u91cd\u542f\u670d\u52a1\u3002\u4f46\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\uff1a<\/p>\n<ul>\n<li>\ud83e\udd14 \u5b83\u662f\u5982\u4f55\u76d1\u542c\u6587\u4ef6\u53d8\u5316\u7684\uff1f<\/li>\n<li>\ud83e\udd14 \u5b83\u662f\u5982\u4f55\u4f18\u96c5\u5730\u6740\u6389\u8fdb\u7a0b\u5e76\u91cd\u542f\u65b0\u8fdb\u7a0b\u7684\uff1f<\/li>\n<\/ul>\n<p>\u4eca\u5929\u6211\u4eec\u5c31\u4ece\u96f6\u5f00\u59cb\uff0c\u624b\u5199\u4e00\u4e2a <strong>Windows \u4e13\u5c5e\u7684\u7b80\u6613 NodeMON \u5de5\u5177<\/strong>\uff0c\u5f7b\u5e95\u641e\u61c2\u80cc\u540e\u7684\u539f\u7406\uff01<\/p>\n<h3>node index.js\u7684\u6267\u884c\u8fc7\u7a0b<\/h3>\n<p>\u6bd4\u5982\u6211\u4f7f\u7528express\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u4ee3\u7801<\/p>\n<pre><code class=\"js\">const express = require('express'); \nconst app = express();\n\napp.get('\/', (req, res) =&gt; { res.send('Hello World!'); });\n\nconst server = app.listen(6666, () =&gt; { \n    console.log('Express \u670d\u52a1\u5df2\u542f\u52a8\uff0c\u8fdb\u7a0b ID\uff1a', process.pid); \/\/ \u6253\u5370\u5f53\u524d\u8fdb\u7a0bID \n    console.log('\u670d\u52a1\u76d1\u542c http:\/\/localhost:6666'); \n});<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u6267\u884c <code>node index.js<\/code> \u65f6\uff1a<\/p>\n<ul>\n<li>\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5728\u73af\u5883\u53d8\u91cf\u7684\u76ee\u5f55\u91cc\u9762\u9010\u4e2a\u67e5\u627e\uff0c\u5e76\u627e\u5230node\u7684\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u6bd4\u5982\u4f4d\u7f6e\u5728\uff1a<code>C:\/Program Files\/nodejs<\/code><\/li>\n<li><img loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"462\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnCpl\" alt=\"\" title=\"\"><\/li>\n<li>\u5f53\u64cd\u4f5c\u7cfb\u7edf\u627e\u5230\u8fd9\u4e2a\u53ef\u6267\u884c\u7a0b\u5e8f\u540e\uff0c\u4f1a\u5b89\u6392\u4e00\u4e2a\u8fdb\u7a0b\u53bb\u52a0\u8f7d\u5e76\u6267\u884c\u8fd9\u4e2anode.exe\u7a0b\u5e8f\uff0c\u518d\u628aindex.js\u6587\u4ef6\u4f5c\u4e3a\u53c2\u6570\u4ea4\u7ed9node.exe\u53bb\u89e3\u6790\u5e76\u8fd0\u884c<\/li>\n<li>\u5728\u8fd9\u4e2a\u65b0\u8fdb\u7a0b\u7684\u5185\u90e8\uff0cNodejs\u4f1a\u53bb\u6267\u884cindex.js\u7684\u4ee3\u7801\uff0c\u6700\u7ec8\u4f1a\u542f\u52a8\u4e00\u4e2aExpress\u670d\u52a1\uff0c\u8fd9\u4e2a\u670d\u52a1\u4f7f\u7528\u7684\u662f6666\u7aef\u53e3<\/li>\n<li>\u53ea\u8981\u8fd9\u4e2a\u8fdb\u7a0b\u4e0d\u7ec8\u6b62\uff08\u6bd4\u5982\u4f60\u6309 <code>Ctrl+C<\/code>\u3001\u6740\u6b7b\u8fdb\u7a0b\u3001\u670d\u52a1\u5668\u91cd\u542f\uff09\uff0c6666 \u7aef\u53e3\u5c31\u4f1a\u4e00\u76f4\u88ab\u5360\u7528\uff0c\u670d\u52a1\u5c31\u4e00\u76f4\u53ef\u7528\u3002<\/li>\n<\/ul>\n<h3>\u901a\u4fd7\u6613\u61c2\u5730\u7c7b\u6bd4\u7406\u89e3<\/h3>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u662f \u201c\u5de5\u5382\u5c0f\u8001\u677f\u201d\uff0c\u8fdb\u7a0b\u662f\u9a7e\u9a76\u673a\u5668\u7684\u4e00\u4e2a\u4e2a\u7684\u5458\u5de5\uff0c\u800cnode.exe \u5219\u662f \u201c\u4e00\u53f0\u673a\u5668\u201d\uff0cindex.js \u662f \u201c\u673a\u5668\u8981\u5904\u7406\u7684\u4efb\u52a1\u5355\u201d<\/p>\n<ul>\n<li><strong>\u64cd\u4f5c\u7cfb\u7edf = \u5de5\u5382\u5c0f\u8001\u677f<\/strong>\u8d1f\u8d23\u7edf\u7b79\u5168\u5c40\uff0c\u51b3\u5b9a\u8981\u4e0d\u8981\u62db\u65b0\u5458\u5de5\uff08\u521b\u5efa\u8fdb\u7a0b\uff09\u3001\u7ed9\u5458\u5de5\u5206\u914d\u673a\u5668\uff08\u8fd0\u884c\u53ef\u6267\u884c\u7a0b\u5e8f\uff09\u3001\u4e0b\u53d1\u4efb\u52a1\u5355\uff08\u4f20\u9012\u53c2\u6570\uff09\uff0c\u8fd8\u80fd\u968f\u65f6\u76d1\u7763\u5458\u5de5\u5de5\u4f5c\u72b6\u6001\uff08\u67e5\u770b\u8fdb\u7a0b PID\u3001\u5360\u7528\u8d44\u6e90\uff09\uff0c\u6216\u8005\u8ba9\u5458\u5de5\u4e0b\u73ed\uff08\u7ec8\u6b62\u8fdb\u7a0b\uff09\u3002<\/li>\n<li><strong>\u8fdb\u7a0b = \u9a7e\u9a76\u673a\u5668\u7684\u5458\u5de5<\/strong>\u662f\u8001\u677f\uff08\u7cfb\u7edf\uff09\u4e13\u95e8\u62db\u6765\u7684 \u201c\u4e13\u4eba\u201d\uff0c\u6709\u81ea\u5df1\u7684\u552f\u4e00\u5de5\u53f7\uff08PID\uff09\uff0c\u4ed6\u7684\u6838\u5fc3\u5de5\u4f5c\u5c31\u662f\u64cd\u4f5c\u624b\u91cc\u7684\u673a\u5668\uff0c\u5168\u7a0b\u53ea\u56f4\u7ed5\u8fd9\u4e2a\u673a\u5668\u548c\u4efb\u52a1\u8f6c\uff0c\u4e0d\u4f1a\u540c\u65f6\u5e72\u522b\u7684\u6d3b\u3002<\/li>\n<li><strong>node.exe = \u5458\u5de5\u9a7e\u9a76\u7684\u673a\u5668<\/strong>\u662f\u5458\u5de5\u7684 \u201c\u5de5\u5177\u201d\uff0c\u672c\u8eab\u6709\u56fa\u5b9a\u7684\u529f\u80fd\uff08JavaScript \u89e3\u91ca\u6267\u884c\u80fd\u529b\uff09\uff0c\u6ca1\u6709\u5458\u5de5\uff08\u8fdb\u7a0b\uff09\u64cd\u4f5c\u7684\u8bdd\uff0c\u5b83\u5c31\u662f\u4e00\u53f0\u95f2\u7f6e\u7684\u673a\u5668\uff0c\u5565\u4e5f\u5e72\u4e0d\u4e86\u3002<\/li>\n<li><strong>index.js = \u673a\u5668\u8981\u5904\u7406\u7684\u4efb\u52a1\u5355<\/strong>\u4e0a\u9762\u5199\u7740\u5177\u4f53\u7684\u5de5\u4f5c\u5185\u5bb9\uff08\u6bd4\u5982 \u201c\u542f\u52a8 Express \u670d\u52a1\u3001\u76d1\u542c 3000 \u7aef\u53e3\u201d\uff09\uff0c\u5458\u5de5\uff08\u8fdb\u7a0b\uff09\u64cd\u4f5c\u673a\u5668\uff08node.exe\uff09\u65f6\uff0c\u5c31\u7167\u7740\u4efb\u52a1\u5355\u7684\u8981\u6c42\u4e00\u6b65\u6b65\u6267\u884c\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u81f3\u4e8e\u7ebf\u7a0b\uff0c\u6211\u4eec\u77e5\u9053\u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u64cd\u4f5c\u673a\u5668\u7684\u5458\u5de5\uff0c\u53ef\u4ee5\u957f\u51fa\u591a\u53ea\u624b\uff0c\u4e24\u53ea\u624b\u5e72\u6d3b\u603b\u4f1a\u6bd4\u4e00\u53ea\u624b\u5e72\u6d3b\u5feb\uff09<\/p>\n<p>\u5458\u5de5\u5e38\u5e38\u7528\u4e00\u53ea\u624b\u5e72\u6d3b\uff08\u8fdb\u7a0b\u4e2d\u7684\u4e3b\u7ebf\u7a0b\u5e72\u6d3b\uff09<\/p>\n<\/blockquote>\n<p><strong>\u8bf4\u5230\u8fd9\u91cc\uff0c\u5c31\u8981\u989d\u5916\u63d0\u4e00\u4e0bProcess\u8fd9\u4e2a\u53d8\u91cf\u4e86<\/strong><\/p>\n<p>process\u5c31\u76f8\u5f53\u4e8enode.exe\u8fd9\u4e2a\u673a\u5668\u4e0a\u9762\u7684\u63a7\u5236\u663e\u793a\u5c4f\uff0c\u8bb0\u5f55\u4e86\u4e00\u4e9b\u4fe1\u606f\uff0c\u63d0\u4f9b\u7ed9\u5916\u90e8\u65b9\u4fbf\u4f7f\u7528<\/p>\n<table>\n<thead>\n<tr>\n<th>\u63a7\u5236\u663e\u793a\u5c4f\uff08process\uff09\u7684\u529f\u80fd<\/th>\n<th>\u5bf9\u5e94\u5de5\u5382\u573a\u666f<\/th>\n<th>\u4ee3\u7801\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u663e\u793a \u201c\u5458\u5de5\u5de5\u53f7\uff08PID\uff09\u201d<\/td>\n<td>\u5c4f\u5e55\u4e0a\u663e\u793a\u5f53\u524d\u64cd\u4f5c\u673a\u5668\u7684\u5458\u5de5\u7f16\u53f7<\/td>\n<td><code>process.pid<\/code> \u2192 \u8f93\u51fa 39900<\/td>\n<\/tr>\n<tr>\n<td>\u663e\u793a \u201c\u673a\u5668\u8fd0\u884c\u53c2\u6570\u201d<\/td>\n<td>\u5c4f\u5e55\u663e\u793a\u673a\u5668\u63a5\u6536\u5230\u7684\u4efb\u52a1\u5355\uff08index.js\uff09\u3001\u542f\u52a8\u6307\u4ee4<\/td>\n<td><code>process.argv<\/code> \u2192 \u8f93\u51fa <code>['node', 'index.js']<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u663e\u793a \u201c\u673a\u5668\u8d44\u6e90\u5360\u7528\u201d<\/td>\n<td>\u5c4f\u5e55\u663e\u793a\u673a\u5668\u5f53\u524d\u7528\u4e86\u591a\u5c11\u5185\u5b58\u3001CPU<\/td>\n<td><code>process.memoryUsage()<\/code> \u2192 \u8f93\u51fa\u5185\u5b58\u5360\u7528\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>\u63d0\u4f9b \u201c\u5173\u673a\u6309\u94ae\u201d<\/td>\n<td>\u5c4f\u5e55\u4e0a\u7684 \u201c\u505c\u6b62\u8fd0\u884c\u201d \u6309\u94ae\uff0c\u6309\u4e86\u5458\u5de5\u5c31\u4e0b\u73ed<\/td>\n<td><code>process.exit()<\/code> \u2192 \u7ec8\u6b62\u5f53\u524d\u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td>\u663e\u793a \u201c\u5de5\u5382\u73af\u5883\u201d<\/td>\n<td>\u5c4f\u5e55\u663e\u793a\u8001\u677f\uff08\u7cfb\u7edf\uff09\u7ed9\u7684\u73af\u5883\u53d8\u91cf\uff08\u6bd4\u5982 PATH\uff09<\/td>\n<td><code>process.env<\/code> \u2192 \u8f93\u51fa\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u6bd4\u5982\uff0c\u6211\u53ef\u4ee5\u6253\u5370\u8fdb\u7a0bid\u5c31\u662f<\/li>\n<li>console.log(&#8216;\u8fdb\u7a0b ID\uff1a&#8217;, process.pid) \/\/ \u5f97\u523039900\u8fd9\u4e2a\u503c \u662f40188\u5f53\u7136\u6bcf\u6b21\u4e00\u822c\u90fd\u4e0d\u4e00\u6837<\/li>\n<\/ul>\n<p><em>cmd\u547d\u4ee4\uff1a<\/em><\/p>\n<p><code>tasklist | findstr 39900<\/code> \u67e5\u770b\u8fdb\u7a0b39900\u5728\u4f7f\u7528\u54ea\u4e00\u53f0\u673a\u5668\u2014\u2014\u4f7f\u7528node.exe\u8fd9\u4e2a\u673a\u5668<\/p>\n<pre><code class=\"bash\">C:\/Users\/lss13&gt;tasklist | findstr 39900\nnode.exe                     39900 Console                    1     25,176 K\n<\/code><\/pre>\n<p><code>netstat -ano | findstr :6666<\/code> \u67e5\u770b6666\u7aef\u53e3\uff0c\u88ab\u90a3\u4e2a\u8fdb\u7a0b\u4f7f\u7528\u2014\u2014\u88ab39900\u8fdb\u7a0b\u4f7f\u7528<\/p>\n<pre><code class=\"bash\">C:\/Users\/lss13&gt;netstat -ano | findstr :6666\n  TCP    0.0.0.0:6666           0.0.0.0:0              LISTENING       39900\n  TCP    [::]:6666              [::]:0                 LISTENING       39900<\/code><\/pre>\n<hr>\n<h2>\u4e00\u3001\u539f\u7406\u8bb2\u89e3<\/h2>\n<p>\u6709\u4e86\u524d\u7f6e\u7684\u77e5\u8bc6\u540e\uff0c\u6211\u4eec\u6765\u68b3\u7406\u4e00\u4e0b\u624b\u5199\u542f\u52a8\u76d1\u63a7\u5de5\u5177\u7684\u601d\u8def<\/p>\n<h3>\u6838\u5fc3\u6d41\u7a0b<\/h3>\n<pre><code>\u542f\u52a8\u76d1\u63a7\u5de5\u5177\n    \u2193\n\u76d1\u542c\u6587\u4ef6\u53d8\u5316\uff08chokidar\uff09\n    \u2193\n\u68c0\u6d4b\u5230\u4fee\u6539 \u2192 \u8ba1\u7b97\u6587\u4ef6 Hash\n    \u2193\nHash \u53d8\u4e86\uff1f\n    \u251c\u2500 \u662f \u2192 \u6740\u6389\u65e7\u8fdb\u7a0b\uff08taskkill\uff09\u2192 \u7b49\u5f85\u7aef\u53e3\u91ca\u653e \u2192 \u542f\u52a8\u65b0\u8fdb\u7a0b\n    \u2514\u2500 \u5426 \u2192 \u5ffd\u7565\uff08\u907f\u514d\u65e0\u610f\u4e49\u91cd\u542f\uff09\n<\/code><\/pre>\n<h3>\u5173\u952e\u6280\u672f\u70b9<\/h3>\n<p>chokidar\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=HV4aSPTsOAJkXfSSa9oJkQ%3D%3D.HGzN9PfhjGE8n3VD4Xl4SJaflrXvu43wGaL%2BQJ9DZnS4RAfYb0tpSzvVz49ZzUcw\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/www.npmjs.com\/package\/chokidar\/v\/4.0.1<\/a><\/p>\n<table>\n<thead>\n<tr>\n<th>\u6280\u672f<\/th>\n<th>\u4f5c\u7528<\/th>\n<th>Windows \u7279\u6b8a\u5904\u7406<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>chokidar<\/strong><\/td>\n<td>\u76d1\u542c\u6587\u4ef6\u53d8\u5316<\/td>\n<td>\u53ef\u9009\u8f6e\u8be2\u6a21\u5f0f\uff08\u66f4\u7a33\u5b9a\uff09<\/td>\n<\/tr>\n<tr>\n<td><strong>spawn<\/strong><\/td>\n<td>\u542f\u52a8\/\u7ba1\u7406\u5b50\u8fdb\u7a0b<\/td>\n<td>\u9700\u8981\u5904\u7406\u8fdb\u7a0b\u6811<\/td>\n<\/tr>\n<tr>\n<td><strong>taskkill<\/strong><\/td>\n<td>\u6740\u6b7b\u8fdb\u7a0b<\/td>\n<td>Windows \u4e13\u5c5e\u547d\u4ee4<\/td>\n<\/tr>\n<tr>\n<td><strong>crypto<\/strong><\/td>\n<td>\u8ba1\u7b97\u6587\u4ef6 Hash<\/td>\n<td>\u7cbe\u786e\u5224\u65ad\u5185\u5bb9\u662f\u5426\u53d8\u5316<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<h3>\u9879\u76ee\u7ed3\u6784<\/h3>\n<pre><code>myNodeMon\/\n\u251c\u2500\u2500 package.json    # \u9879\u76ee\u914d\u7f6e\n\u251c\u2500\u2500 nmon.js         # \u76d1\u63a7\u5de5\u5177\uff08\u6838\u5fc3\uff09\n\u2514\u2500\u2500 index.js        # \u4e1a\u52a1\u4ee3\u7801\uff08HTTP \u670d\u52a1\u5668\uff09\n<\/code><\/pre>\n<h3>\u7b2c\u4e00\u6b65\uff1a\u521d\u59cb\u5316\u9879\u76ee<\/h3>\n<pre><code class=\"bash\"># \u521b\u5efa\u9879\u76ee\u76ee\u5f55\nmkdir myNodeMon\ncd myNodeMon\n\n# \u521d\u59cb\u5316 package.json\nnpm init -y\n\n# \u5b89\u88c5\u4f9d\u8d56\nnpm install chokidar<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u6b65\uff1a\u914d\u7f6e package.json<\/h3>\n<pre><code class=\"json\">{\n  &quot;name&quot;: &quot;mynodemon&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;type&quot;: &quot;module&quot;,\n  &quot;author&quot;: &quot;&quot;,\n  &quot;license&quot;: &quot;ISC&quot;,\n  &quot;description&quot;: &quot;&quot;,\n  &quot;dependencies&quot;: {\n    &quot;chokidar&quot;: &quot;^5.0.0&quot;\n  }\n}<\/code><\/pre>\n<p><strong>\u5173\u952e\u914d\u7f6e\uff1a<\/strong><\/p>\n<ul>\n<li><code>&quot;type&quot;: &quot;module&quot;<\/code>\uff1a\u542f\u7528 ES Module \u8bed\u6cd5<\/li>\n<\/ul>\n<hr>\n<h3>\u7b2c\u4e09\u6b65\uff1a\u7f16\u5199\u4e1a\u52a1\u4ee3\u7801\uff08index.js\uff09<\/h3>\n<p>\u8fd9\u662f\u6211\u4eec\u8981\u76d1\u63a7\u7684\u76ee\u6807\u6587\u4ef6index.js\uff0c\u4e00\u4e2a\u7b80\u5355\u7684 HTTP \u670d\u52a1\u5668\uff1a<\/p>\n<pre><code class=\"javascript\">import http from 'http';\n\n\/\/ \u521b\u5efahttp\u670d\u52a1\u5668\nconst server = http.createServer((_req, res) =&gt; {\n    res.writeHead(200, { 'Content-Type': 'text\/plain; charset=utf-8' });\n    res.end(`\n    \u5f53\u524d\u65f6\u95f4\uff1a${new Date().toLocaleTimeString()}\n  `);\n});\n\nconsole.log(666)\n\n\/\/ \u542f\u52a8\u670d\u52a1\u5668\nserver.listen(6666, () =&gt; {\n    console.log(`Node.js \u670d\u52a1\u5df2\u542f\u52a8\uff1ahttp:\/\/localhost:6666`);\n});<\/code><\/pre>\n<p><em>\u529f\u80fd\uff1a<\/em><\/p>\n<ul>\n<li>\u76d1\u542c 6666 \u7aef\u53e3<\/li>\n<li>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/li>\n<\/ul>\n<hr>\n<h3>\u7b2c\u56db\u6b65\uff1a\u7f16\u5199\u76d1\u63a7\u5de5\u5177\uff08nmon.js\uff09<\/h3>\n<p>\u8fd9\u662f\u6838\u5fc3\u4ee3\u7801\uff0c\u6211\u4eec\u9010\u5757\u89e3\u6790\uff1a<\/p>\n<p><em>\u5bfc\u5165\u4f9d\u8d56<\/em><\/p>\n<pre><code class=\"javascript\">import chokidar from 'chokidar'; \/\/ \u6587\u4ef6\u76d1\u542c\u5e93\nimport { spawn } from 'child_process'; \/\/ \u5b50\u8fdb\u7a0b\u7ba1\u7406\nimport path from 'path';\nimport crypto from 'crypto'; \/\/ Hash \u8ba1\u7b97\nimport fs from 'fs'; \/\/ \u6587\u4ef6\u8bfb\u53d6<\/code><\/pre>\n<hr>\n<p><em>\u914d\u7f6e\u9879<\/em><\/p>\n<pre><code class=\"javascript\">\/\/ ===================== \u914d\u7f6e\u9879 =====================\nconst TARGET_FILE = 'index.js'; \/\/ \u8981\u76d1\u63a7\u7684\u6587\u4ef6\n\/\/ =================================================\n\nconst entryPath = path.resolve(process.cwd(), TARGET_FILE); \/\/ \u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<\/code><\/pre>\n<ul>\n<li><code>process.cwd()<\/code>\uff1a\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff08\u8fd0\u884c\u547d\u4ee4\u7684\u4f4d\u7f6e\uff09<\/li>\n<li><code>path.resolve()<\/code>\uff1a\u62fc\u63a5\u6210\u7edd\u5bf9\u8def\u5f84\uff0c\u5982 <code>C:\/Users\/xxx\/myNodeMon\/index.js<\/code><\/li>\n<\/ul>\n<hr>\n<p><em>Hash \u8ba1\u7b97<\/em><\/p>\n<pre><code class=\"javascript\">let lastHash = null; \/\/ \u4fdd\u5b58\u4e0a\u6b21\u7684 hash\n\nfunction getFileHash(filePath) {\n    const content = fs.readFileSync(filePath);\n    return crypto.createHash('md5').update(content).digest('hex');\n}<\/code><\/pre>\n<p><em>\u4e3a\u4ec0\u4e48\u8981\u7528 Hash\uff1f<\/em><\/p>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6848<\/th>\n<th>\u95ee\u9898<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u53ea\u68c0\u6d4b\u4fee\u6539\u65f6\u95f4<\/td>\n<td>Ctrl+S \u4e0d\u4fee\u6539\u5185\u5bb9\u4e5f\u4f1a\u89e6\u53d1\u91cd\u542f \u274c<\/td>\n<\/tr>\n<tr>\n<td><strong>Hash \u5bf9\u6bd4<\/strong><\/td>\n<td>\u53ea\u6709\u5185\u5bb9\u771f\u6b63\u53d8\u5316\u624d\u91cd\u542f \u2705<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>\u5f53\u6211\u4eec\u5728\u7f16\u8f91\u5668\u91cc\u9762 Ctrl+S \u7684\u65f6\u5019\uff0c\u5c3d\u7ba1\u6ca1\u6709\u4fee\u6539\u6587\u4ef6\uff0c\u4f46\u662f\u64cd\u4f5c\u7cfb\u7edf\u4f9d\u65e7\u8ba4\u4e3a\u8fd9\u4e2a\u6587\u4ef6\u53d8\u5316\u4e86\uff0c\u4e5f\u4f1a\u89e6\u53d1\u6587\u4ef6\u53d8\u5316\u56de\u8c03\u51fd\u6570\uff0c\u5373\u6587\u4ef6\u7684\u65f6\u95f4\u4f1a\u53d8\u5316<\/p><\/blockquote>\n<p><em>\u5de5\u4f5c\u539f\u7406\uff1a<\/em><\/p>\n<ol>\n<li>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/li>\n<li>\u8ba1\u7b97 MD5 \u54c8\u5e0c\u503c\uff08\u5982 <code>a1b2c3d4...<\/code>\uff09<\/li>\n<li>\u5bf9\u6bd4\u4e0a\u6b21\u7684\u54c8\u5e0c\u503c<\/li>\n<li>\u4e0d\u540c\u624d\u91cd\u542f<\/li>\n<\/ol>\n<hr>\n<p><em>\u8fdb\u7a0b\u5f15\u7528\u7ba1\u7406<\/em><\/p>\n<pre><code class=\"javascript\">let childProcess = null; \/\/ \u4fdd\u5b58\u8fdb\u7a0b\u5b9e\u4f8b\n\n\/\/ \u542f\u52a8\u6216\u91cd\u542f index.js\nfunction startApp() {\n    if (childProcess) {\n        console.log('\ud83d\udd04 \u6b63\u5728\u7ec8\u6b62\u65e7\u8fdb\u7a0b...');\n\n        \/\/ Windows \u4e0b\u7528 taskkill \u5f3a\u5236\u6740\u6b7b\u8fdb\u7a0b\u6811\n        const killProcess = spawn('taskkill', ['\/F', '\/T', '\/PID', childProcess.pid]);\n\n        killProcess.on('close', () =&gt; {\n            childProcess = null; \/\/ \u6e05\u7a7a\u5f15\u7528\n            setTimeout(() =&gt; {\n                launchNewProcess();\n            }, 200); \/\/ \u7b49\u5f85\u7aef\u53e3\u91ca\u653e\n        });\n        return;\n    }\n    launchNewProcess();\n}<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>\/F<\/code><\/td>\n<td>\u5f3a\u5236\u7ec8\u6b62<\/td>\n<\/tr>\n<tr>\n<td><code>\/T<\/code><\/td>\n<td>\u7ec8\u6b62\u8fdb\u7a0b\u6811\uff08\u5305\u62ec\u5b50\u8fdb\u7a0b\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>\/PID<\/code><\/td>\n<td>\u6309\u8fdb\u7a0b ID \u6740\u6b7b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em>\u4e3a\u4ec0\u4e48\u8981\u5ef6\u8fdf 200ms\uff1f<\/em><\/p>\n<pre><code>taskkill \u5b8c\u6210 \u2192 \u8fdb\u7a0b\u9000\u51fa \u2192 \u64cd\u4f5c\u7cfb\u7edf\u91ca\u653e\u7aef\u53e3 \u2192 \u65b0\u8fdb\u7a0b\u542f\u52a8\n                              \u2191\n                        \u8fd9\u91cc\u9700\u8981\u65f6\u95f4\uff08\u7ea6 50-150ms\uff09\n<\/code><\/pre>\n<p>\u5982\u679c\u4e0d\u7b49\u5f85\uff0c\u65b0\u8fdb\u7a0b\u4f1a\u62a5\u9519\uff1a<code>EADDRINUSE: address already in use<\/code><\/p>\n<hr>\n<p><em>\u542f\u52a8\u65b0\u8fdb\u7a0b<\/em><\/p>\n<pre><code class=\"javascript\">function launchNewProcess() {\n    console.log('\ud83d\ude80 \u6b63\u5728\u542f\u52a8\u65b0\u8fdb\u7a0b...\/n');\n\n    \/\/ \u76f8\u5f53\u4e8e\u6267\u884c\uff1anode index.js\n    childProcess = spawn('node', [entryPath], {\n        stdio: 'inherit' \/\/ \u8ba9\u5b50\u8fdb\u7a0b\u7684\u65e5\u5fd7\u76f4\u63a5\u663e\u793a\u5728\u63a7\u5236\u53f0\n    });\n\n    childProcess.on('error', (err) =&gt; {\n        console.error('\u274c \u542f\u52a8\u5931\u8d25\uff1a', err.message);\n    });\n}<\/code><\/pre>\n<p><code>stdio: 'inherit'<\/code>\uff1a\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u8f93\u5165\u8f93\u51fa\uff0c\u8ba9 <code>index.js<\/code> \u7684\u65e5\u5fd7\u80fd\u663e\u793a\u51fa\u6765<\/p>\n<hr>\n<p><em>\u6587\u4ef6\u53d8\u5316\u76d1\u542c<\/em><\/p>\n<pre><code class=\"javascript\">\/\/ \u521b\u5efa\u6587\u4ef6\u76d1\u542c\u5668\nconst watcher = chokidar.watch(TARGET_FILE, {\n    ignoreInitial: true  \/\/ \u5ffd\u7565\u521d\u59cb\u5316\u65f6\u7684\u4e8b\u4ef6\n});\n\nwatcher.on('change', () =&gt; {\n    const currentHash = getFileHash(TARGET_FILE);\n    \n    if (currentHash !== lastHash) {\n        console.log('\ud83d\udcdd \u68c0\u6d4b\u5230\u5185\u5bb9\u771f\u7684\u53d8\u4e86');\n        lastHash = currentHash;\n        startApp();\n    } else {\n        console.log('\u23ed\ufe0f \u5185\u5bb9\u6ca1\u53d8\uff0c\u5ffd\u7565');\n    }\n});\n\n\/\/ \u521d\u59cb\u5316\u65f6\u8ba1\u7b97\u4e00\u6b21\nlastHash = getFileHash(TARGET_FILE);<\/code><\/pre>\n<p><em>\u6d41\u7a0b<\/em><\/p>\n<ol>\n<li>\u76d1\u542c <code>index.js<\/code> \u7684 <code>change<\/code> \u4e8b\u4ef6<\/li>\n<li>\u8ba1\u7b97\u5f53\u524d\u6587\u4ef6\u7684 Hash<\/li>\n<li>\u5bf9\u6bd4\u4e0a\u6b21\u7684 Hash<\/li>\n<li>\u4e0d\u540c\u624d\u8c03\u7528 <code>startApp()<\/code> \u91cd\u542f<\/li>\n<\/ol>\n<hr>\n<p><em>\u542f\u52a8\u76d1\u63a7<\/em><\/p>\n<pre><code class=\"javascript\">console.log(`\ud83d\ude80 Windows \u76d1\u63a7\u5de5\u5177\u5df2\u542f\u52a8`);\nconsole.log(`\ud83c\udfaf \u76d1\u63a7\u76ee\u6807\uff1a${TARGET_FILE}\/n`);\nstartApp();<\/code><\/pre>\n<hr>\n<p><em>\u4f18\u96c5\u9000\u51fa\uff08Ctrl+C\uff09<\/em><\/p>\n<pre><code class=\"javascript\">process.on('SIGINT', () =&gt; {\n    if (childProcess) {\n        console.log('\/n\ud83d\uded1 \u6b63\u5728\u6e05\u7406\u5b50\u8fdb\u7a0b...');\n        spawn('taskkill', ['\/F', '\/T', '\/PID', childProcess.pid]);\n        childProcess = null;\n    }\n    watcher.close();\n    console.log('\ud83d\udc4b \u76d1\u63a7\u5de5\u5177\u5df2\u9000\u51fa');\n    process.exit(0);\n});<\/code><\/pre>\n<ul>\n<li>\u6309 Ctrl+C \u65f6\u6e05\u7406\u5b50\u8fdb\u7a0b<\/li>\n<li>\u5173\u95ed\u6587\u4ef6\u76d1\u542c\u5668<\/li>\n<li>\u907f\u514d\u7aef\u53e3\u6b8b\u7559\u5360\u7528<\/li>\n<\/ul>\n<hr>\n<h2>\u4e09\u3001\u6d4b\u8bd5\u9a8c\u8bc1\u6548\u679c<\/h2>\n<p>\u6d4b\u8bd5\u573a\u666f 0\uff1a\u542f\u52a8\u670d\u52a1\u5e76\u8bbf\u95ee<br \/>\u6d4b\u8bd5\u573a\u666f 1\uff1a\u4fee\u6539\u6587\u4ef6\u5185\u5bb9<br \/>\u6d4b\u8bd5\u573a\u666f 2\uff1a\u4e0d\u4fee\u6539\u5185\u5bb9\uff0c\u53ea\u4fdd\u5b58<br \/>\u6d4b\u8bd5\u573a\u666f 3\uff1a\u4f18\u96c5\u9000\u51fa<\/p>\n<p>\u6548\u679c\u56fe<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"694\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnCpm\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<hr>\n<h2>\u56db\u3001\u8e29\u5751<\/h2>\n<h3>\u4e3a\u4ec0\u4e48 Windows \u9700\u8981\u7279\u6b8a\u5904\u7406\uff1f<\/h3>\n<p><em>\u95ee\u9898 1\uff1a\u6587\u4ef6\u76d1\u542c\u4e0d\u7a33\u5b9a<\/em><\/p>\n<p>Linux\/Mac\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>inotify<\/code> \/ <code>FSEvents<\/code>\uff08\u5185\u6838\u7ea7\u522b\uff09<\/li>\n<li>\u9ad8\u6548\u3001\u51c6\u786e\u3001\u5b9e\u65f6<\/li>\n<\/ul>\n<p>Windows\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>ReadDirectoryChangesW<\/code>\uff08\u57fa\u4e8e\u76ee\u5f55\u626b\u63cf\uff09<\/li>\n<li>\u5bb9\u6613\u4e22\u5931\u4e8b\u4ef6\u6216\u91cd\u590d\u89e6\u53d1<\/li>\n<li>\u5bb9\u6613\u4e22\u5931\u4e8b\u4ef6\u6216\u91cd\u590d\u89e6\u53d1<\/li>\n<li>\u5bb9\u6613\u4e22\u5931\u4e8b\u4ef6\u6216\u91cd\u590d\u89e6\u53d1<\/li>\n<\/ul>\n<blockquote><p><strong>\u7279\u522b\u662f\u5feb\u901fCtrl + S\u4fdd\u5b58\uff0c\u53ef\u80fd\u4f1a\u4e8b\u4ef6\u8bef\u89e6\u53d1&#8230;<\/strong><\/p><\/blockquote>\n<p>\u89e3\u51b3\u65b9\u6848\u53ef\u9009\uff1a<\/p>\n<pre><code class=\"javascript\">\/\/ \u53ef\u9009\uff1a\u4f7f\u7528\u8f6e\u8be2\u6a21\u5f0f\uff08\u66f4\u7a33\u5b9a\u4f46\u8017\u8d44\u6e90\uff09\nconst watcher = chokidar.watch(TARGET_FILE, {\n    ignoreInitial: true,\n    usePolling: true,     \/\/ \u5f3a\u5236\u8f6e\u8be2\n    interval: 500         \/\/ \u6bcf 500ms \u68c0\u67e5\u4e00\u6b21\n});<\/code><\/pre>\n<hr>\n<p><em>\u95ee\u9898 2\uff1a\u8fdb\u7a0b\u6740\u4e0d\u5e72\u51c0<\/em><\/p>\n<p>Linux\/Mac\uff1a<\/p>\n<pre><code class=\"bash\">kill -9 &lt;PID&gt;  # \u76f4\u63a5\u6740\u8fdb\u7a0b<\/code><\/pre>\n<p>Windows\uff1a<\/p>\n<pre><code class=\"bash\">taskkill \/F \/T \/PID &lt;PID&gt;  # \u9700\u8981\u6740\u8fdb\u7a0b\u6811<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u8981\u52a0 <code>\/T<\/code>\uff1f<\/p>\n<pre><code>\u7236\u8fdb\u7a0b\uff08node nmon.js\uff09\n  \u2514\u2500 \u5b50\u8fdb\u7a0b\uff08node index.js\uff09\n       \u2514\u2500 \u53ef\u80fd\u8fd8\u6709\u5b59\u8fdb\u7a0b\n<\/code><\/pre>\n<p>\u4e0d\u52a0 <code>\/T<\/code> \u53ea\u6740\u7236\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u4f1a\u53d8\u6210\u5b64\u513f\u8fdb\u7a0b\uff0c\u7ee7\u7eed\u5360\u7528\u7aef\u53e3\u3002<\/p>\n<hr>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u4fdd\u5b58\u8fdb\u7a0b\u5b9e\u4f8b\uff1f<\/h3>\n<pre><code class=\"javascript\">let childProcess = null;<\/code><\/pre>\n<p><strong>\u4f5c\u7528\uff1a<\/strong><\/p>\n<ol>\n<li>\u83b7\u53d6\u8fdb\u7a0b PID\uff08<code>childProcess.pid<\/code>\uff09<\/li>\n<li>\u5728\u91cd\u542f\u65f6\u6740\u6389\u65e7\u8fdb\u7a0b<\/li>\n<li>\u76d1\u542c\u8fdb\u7a0b\u72b6\u6001\uff08\u9000\u51fa\u3001\u9519\u8bef\uff09<\/li>\n<\/ol>\n<p><strong>\u5982\u679c\u4e0d\u4fdd\u5b58\uff1a<\/strong><\/p>\n<pre><code class=\"javascript\">\/\/ \u274c \u9519\u8bef\u793a\u8303\nspawn('node', ['index.js']); \/\/ \u542f\u52a8\u4e86\uff0c\u4f46\u6ca1\u4eba\u8bb0\u4f4f\u5b83\n\n\/\/ \u60f3\u91cd\u542f\u65f6\nspawn('taskkill', ['\/PID', ???]); \/\/ \u4e0d\u77e5\u9053 PID\uff0c\u65e0\u6cd5\u6740\u8fdb\u7a0b<\/code><\/pre>\n<hr>\n<h2>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801<\/h2>\n<h3>GitHub<\/h3>\n<p>\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=4UxZYCnZiPPbBNcaJG3Ttw%3D%3D.yKGOtu50VPMezjUoSR%2F0Jp96Zb2KguPRkGMH9jwIjMK1CUrFg3fddvYz%2BgwCVF5w\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/shuirongshuifu\/mynodemon<\/a><\/p>\n<h3>nmon.js<\/h3>\n<pre><code class=\"javascript\">import chokidar from 'chokidar'; \/\/ \u76d1\u63a7\u5305\nimport { spawn } from 'child_process'; \/\/ \u6d3e\u53d1\u751f\u7cfb\u7edf\u547d\u4ee4\u6765\u521b\u5efa\u548c\u7ec8\u6b62\u5b50\u8fdb\u7a0b\uff0c\u5b9e\u73b0\u542f\u52a8\u548c\u91cd\u542f\nimport path from 'path';\nimport crypto from 'crypto';\nimport fs from 'fs';\n\n\/\/ ===================== \u914d\u7f6e\u9879 =====================\nconst TARGET_FILE = 'index.js'; \/\/ \u8981\u76d1\u63a7\u5e76\u81ea\u52a8\u91cd\u542f\u7684\u6587\u4ef6\n\/\/ =================================================\n\nconst entryPath = path.resolve(process.cwd(), TARGET_FILE); \/\/ \u8def\u5f84\n\nlet childProcess = null; \/\/ \u4fdd\u5b58 index.js \u7684\u8fdb\u7a0b\u5b9e\u4f8b\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4fbf\u4e8e\u540e\u7eed\u6e05\u7a7a\u91cd\u7f6e\n\nlet lastHash = null; \/\/ \u4fdd\u5b58\u4e0a\u6b21\u7684 hash\n\nfunction getFileHash(filePath) {\n    const content = fs.readFileSync(filePath);\n    return crypto.createHash('md5').update(content).digest('hex');\n}\n\n\/\/ \u542f\u52a8\u6216\u91cd\u542f index.js\nfunction startApp() {\n    \/\/ \u5f53\u524d\u6709\u8fdb\u7a0b\uff0c\u5c31\u6e05\u9664\u6389\u4ee5\u540e\uff0c\u518d\u542f\u52a8\uff08\u91cd\u542f\u529f\u80fd\uff09\n    if (childProcess) {\n        console.log('\ud83d\udd04 \u6b63\u5728\u7ec8\u6b62\u65e7\u8fdb\u7a0b...');\n\n        \/\/ Windows \u4e0b\u7528 taskkill\u547d\u4ee4 \u5f3a\u5236\u6740\u6b7b\u8fdb\u7a0b\u6811 \/\/ \u6bd4\u5982\u7c7b\u4f3c taskkill \/f \/t \/im nginx.exe\n        const killProcess = spawn('taskkill', ['\/F', '\/T', '\/PID', childProcess.pid]);\n\n        killProcess.on('close', () =&gt; {\n            childProcess = null; \/\/ \u6e05\u7a7a\u8fdb\u7a0b\u5b9e\u4f8b\u5bf9\u8c61\u7684\u5f15\u7528\n            setTimeout(() =&gt; { \/\/ \u7b49200\u6beb\u79d2\u8db3\u591f\u64cd\u4f5c\u7cfb\u7edf\u91ca\u653e\u7aef\u53e3\u4e86\n                launchNewProcess();\n            }, 200);\n        });\n        return;\n    }\n    \/\/ \u5f53\u524d\u6ca1\u6709\u8fdb\u7a0b\u5c31\u76f4\u63a5\u542f\u52a8\u5373\u53ef\n    launchNewProcess();\n}\n\n\/\/ \u542f\u52a8\u65b0\u8fdb\u7a0b\nfunction launchNewProcess() {\n    console.log('\ud83d\ude80 \u6b63\u5728\u542f\u52a8\u65b0\u8fdb\u7a0b...\/n');\n\n    \/\/ \u76f8\u5f53\u4e8e\u6267\u884c\u547d\u4ee4\uff1anode C:\/Users\/xxx\/myNodeMon\/index.js \u7b80\u5316\u5c31\u662f node index.js\n    childProcess = spawn('node', [entryPath], {\n        stdio: 'inherit' \/\/ \u8ba9\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u65e5\u5fd7\uff0c\u76f4\u63a5\u663e\u793a\u5728\u5f53\u524d\u63a7\u5236\u53f0\n    });\n\n    \/\/ \u6bd4\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u8005\u8def\u5f84\u9519\u8bef\u4f1a\u62a5\u9519\uff0c\u515c\u4e00\u4e0b\n    childProcess.on('error', (err) =&gt; {\n        console.error('\u274c \u542f\u52a8\u5931\u8d25\uff1a', err.message);\n    });\n}\n\n\/\/ \u521b\u5efa\u6587\u4ef6\u76d1\u542c\u5668\u2014\u2014\u4e0d\u4f7f\u7528\u8f6e\u8be2\nconst watcher = chokidar.watch(TARGET_FILE, {\n    ignoreInitial: true  \/\/ \u5ffd\u7565\u521d\u59cb\u5316\u65f6\u7684\u4e8b\u4ef6\n});\n\n\/\/ \/\/ \u521b\u5efa\u6587\u4ef6\u76d1\u542c\u5668\u2014\u2014\u4f7f\u7528\u8f6e\u8be2\n\/\/ const watcher = chokidar.watch(TARGET_FILE, {\n\/\/     ignoreInitial: true,  \/\/ \u5ffd\u7565\u521d\u59cb\u5316\u65f6\u7684\u4e8b\u4ef6\n\/\/     usePolling: true,     \/\/ Windows\u4e0b\u7528\u8f6e\u8be2\u66f4\u52a0\u7a33\u59a5\uff08\u6bd5\u7adf\u5176\u6587\u4ef6\u7ba1\u7406\u6ca1\u6709Linux\u505a\u5f97\u597d\uff09\n\/\/     interval: 1000         \/\/ \u6bcf 100ms \u68c0\u67e5\u4e00\u6b21\u6587\u4ef6\u53d8\u5316\n\/\/ });\n\nwatcher.on('change', () =&gt; {\n    const currentHash = getFileHash(TARGET_FILE);\n    \n    if (currentHash !== lastHash) {\n        console.log('\ud83d\udcdd \u68c0\u6d4b\u5230\u5185\u5bb9\u771f\u7684\u53d8\u4e86');\n        lastHash = currentHash;\n        startApp();\n    } else {\n        console.log('\u23ed\ufe0f \u5185\u5bb9\u6ca1\u53d8\uff0c\u5ffd\u7565');\n    }\n});\n\n\/\/ \u521d\u59cb\u5316\u65f6\u8ba1\u7b97\u4e00\u6b21\nlastHash = getFileHash(TARGET_FILE);\n\n\/\/ \u542f\u52a8\u76d1\u63a7\nconsole.log(`\ud83d\ude80 Windows \u76d1\u63a7\u5de5\u5177\u5df2\u542f\u52a8`);\nconsole.log(`\ud83c\udfaf \u76d1\u63a7\u76ee\u6807\uff1a${TARGET_FILE}\/n`);\nstartApp();\n\n\/\/ Ctrl+C \u9000\u51fa\u65f6\u6e05\u7406\u8fdb\u7a0b\nprocess.on('SIGINT', () =&gt; {\n    if (childProcess) {\n        console.log('\/n\ud83d\uded1 \u6b63\u5728\u6e05\u7406\u5b50\u8fdb\u7a0b...');\n        spawn('taskkill', ['\/F', '\/T', '\/PID', childProcess.pid]);\n        childProcess = null;\n    }\n    watcher.close();\n    console.log('\ud83d\udc4b \u76d1\u63a7\u5de5\u5177\u5df2\u9000\u51fa');\n    process.exit(0);\n});<\/code><\/pre>\n<hr>\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<h3>\u5b66\u5230\u4e86\u4ec0\u4e48\uff1f<\/h3>\n<ul>\n<li>\u901a\u4fd7\u6613\u61c2\u7406\u89e3\u4e00\u4e9b\u57fa\u7840\u6982\u5ff5<\/li>\n<li>chokidar \u7684\u4f7f\u7528<\/li>\n<li>Windows \u4e0b\u6587\u4ef6\u7cfb\u7edf\u8fd8\u662f\u5dee\u70b9\u610f\u601d\uff08\u7a33\u5b9a\u6027\u95ee\u9898\uff09<\/li>\n<li><code>spawn()<\/code> \u542f\u52a8\u5b50\u8fdb\u7a0b<\/li>\n<li><code>taskkill<\/code> \u6740\u6b7b\u8fdb\u7a0b\u6811<\/li>\n<li>\u901a\u8fc7\u6587\u4ef6hash\u7cbe\u786e\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u53d8\u5316<\/li>\n<li>\u4fe1\u53f7\u5904\u7406\uff08<code>SIGINT<\/code>\uff09<\/li>\n<\/ul>\n<hr>\n<h3>\u4e0e nodemon \u7684\u5bf9\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>\u6211\u4eec\u7684\u5de5\u5177<\/th>\n<th>nodemon<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6587\u4ef6\u76d1\u542c<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u52a8\u91cd\u542f<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>Hash \u5bf9\u6bd4<\/td>\n<td>\u2705<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td>\u914d\u7f6e\u6587\u4ef6<\/td>\n<td>\u274c<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u5e73\u53f0<\/td>\n<td>\u274c\uff08\u4ec5 Windows\uff09<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u5fd7\u8bb0\u5f55<\/td>\n<td>\u274c<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u4ee3\u7801\u91cf<\/td>\n<td>\/~100 \u884c<\/td>\n<td>\/~5000 \u884c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h2>\u4e03\u3001\u62d3\u5c55\u601d\u8003<\/h2>\n<h3>\ud83e\udd14 \u5982\u4f55\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\uff1f<\/h3>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<pre><code class=\"javascript\">const TARGET_FILES = ['index.js', 'config.js'];\nconst watcher = chokidar.watch(TARGET_FILES, { ... });\n\n\/\/ \u9700\u8981\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u4fdd\u5b58 Hash\nconst fileHashes = new Map();<\/code><\/pre>\n<hr>\n<h3>\ud83e\udd14 \u5982\u4f55\u6dfb\u52a0\u65e5\u5fd7\u8f93\u51fa\uff1f<\/h3>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<pre><code class=\"javascript\">import fs from 'fs';\n\nfunction log(message) {\n    const timestamp = new Date().toISOString();\n    const logMessage = `[${timestamp}] ${message}\/n`;\n    fs.appendFileSync('nmon.log', logMessage);\n    console.log(message);\n}<\/code><\/pre>\n<hr>\n<h3>\ud83e\udd14 \u5982\u4f55\u652f\u6301\u914d\u7f6e\u6587\u4ef6\uff1f<\/h3>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<pre><code class=\"javascript\">\/\/ nmon.config.json\n{\n  &quot;target&quot;: &quot;index.js&quot;,\n  &quot;port&quot;: 6666,\n  &quot;delay&quot;: 200\n}\n\n\/\/ \u8bfb\u53d6\u914d\u7f6e\nconst config = JSON.parse(fs.readFileSync('nmon.config.json', 'utf8'));<\/code><\/pre>\n<hr>\n<h3>\ud83e\udd14 \u5982\u4f55\u5b9e\u73b0\u70ed\u91cd\u8f7d\uff08\u4e0d\u91cd\u542f\u8fdb\u7a0b\uff09\uff1f<\/h3>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 <code>vm<\/code> \u6a21\u5757\u52a8\u6001\u52a0\u8f7d\u4ee3\u7801<\/li>\n<li>\u6216\u8005\u4f7f\u7528 WebSocket \u901a\u77e5\u6d4f\u89c8\u5668\u5237\u65b0<\/li>\n<\/ul>\n<hr>\n<h2>\u516b\u3001\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=F9D97UtjSYcqENGjt72AwA%3D%3D.5ctHr0BihfOCTJZFv%2FQczh%2B0B6tvvF1SGpC3UsxOddp6AmTIl96%2BjLo8Pvb5xagE\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">chokidar \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=LNvcjwNNJIzSDevqwr5yWw%3D%3D.nY6A5QJCU0zfRIwBSyqMlRfUy%2BC%2BBRSRObnYUofI%2BJEgPPqREuyhq%2BNtqmONw185\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Node.js child\/_process \u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=kvnFmIUHbQyp5atfg3VZcw%3D%3D.td336o8jQyWTwolv9mIUlFeFZZ%2FClmOixK%2BQUHfgpbQe53GLWcfsOwShbpo4HMBx\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Node.js crypto \u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=Zv6UIbPtNKOmTXdhygp%2F4w%3D%3D.HGmO9bYwbtdwZHSbP3aelwTOFi7gvq%2BvGysIPntDCyh0R9CviBBWGcOdviOhtLyh3yZ6AkASlcWyhUM0WNsQgKysSzSLlpXPfkXcJhmoWIxN3hEnaWyXwwDh0mHv%2F2aW\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Windows taskkill \u547d\u4ee4<\/a><\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047535847\">https:\/\/segmentfault.com\/a\/1190000047535847<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u8981\u624b\u5199 NodeMON\uff1f \u5728\u5f00\u53d1 Node.js \u5e94\u7528\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u7528 nodemon \u6765\u81ea\u52a8\u91cd\u542f\u670d\u52a1\u3002\u4f46 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021186","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021186"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021186\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}