{"id":1442,"date":"2017-10-19T17:51:19","date_gmt":"2017-10-19T09:51:19","guid":{"rendered":"https:\/\/www.strongd.net\/?p=1442"},"modified":"2017-10-19T17:51:19","modified_gmt":"2017-10-19T09:51:19","slug":"express-%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1442","title":{"rendered":"Express \u5165\u95e8"},"content":{"rendered":"<h1 id=\"express-\">Express \u5165\u95e8<\/h1>\n<h2 id=\"-nodejs\">\u5b89\u88c5 NodeJS<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min<\/p><\/blockquote>\n<h3 id=\"-nodejs\">\u5b89\u88c5 NodeJS<\/h3>\n<p>\u5728\u7ec8\u7aef\u4e2d\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5b89\u88c5 NodeJS\uff1a<\/p>\n<pre><code>curl --silent --location https:\/\/rpm.nodesource.com\/setup_8.x | sudo bash -\r\nyum -y install nodejs\r\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6d4b\u8bd5\u5b89\u88c5\u7ed3\u679c\uff1a<\/p>\n<pre><code>node -v\r\n<\/code><\/pre>\n<h2 id=\"-express\">\u5b89\u88c5 Express<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min<\/p><\/blockquote>\n<h3 id=\"-\">\u521b\u5efa\u5de5\u4f5c\u76ee\u5f55<\/h3>\n<p>\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5728\u670d\u52a1\u5668\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<pre><code>mkdir -p \/data\/release\/hello\r\n<\/code><\/pre>\n<p>\u8fdb\u5165\u6b64\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<pre><code>cd \/data\/release\/hello\r\n<\/code><\/pre>\n<h3 id=\"-\">\u521d\u59cb\u5316\u9879\u76ee<\/h3>\n<p>\u901a\u8fc7\u00a0<code>npm init<\/code>\u00a0\u547d\u4ee4\u4e3a\u4f60\u7684\u5e94\u7528\u521b\u5efa\u4e00\u4e2a\u00a0<code>package.json<\/code>\u00a0\u6587\u4ef6\u3002\u6b32\u4e86\u89e3\u00a0<code>package.json<\/code>\u00a0\u662f\u5982\u4f55\u8d77\u4f5c\u7528\u7684\uff0c\u8bf7\u53c2\u8003\u00a0<em>Specifics of npm\u2019s package.json handling<\/em>\u3002<\/p>\n<pre><code>npm init\r\n<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c06\u8981\u6c42\u4f60\u8f93\u5165\u51e0\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982\u6b64\u5e94\u7528\u7684\u540d\u79f0\u548c\u7248\u672c\u3002 \u9664\u00a0<code>entry point: (index.js)<\/code>\u00a0\u53c2\u6570\u5916\uff0c\u5176\u4ed6\u53c2\u6570\u4f60\u53ef\u4ee5\u76f4\u63a5\u6309 \u201c<strong>\u56de\u8f66<\/strong>\u201d \u952e\u63a5\u53d7\u9ed8\u8ba4\u8bbe\u7f6e\u5373\u53ef\u3002<\/p>\n<p>\u5bf9\u4e8e\u00a0<code>entry point: (index.js)<\/code>\u00a0\u53c2\u6570\uff0c\u952e\u5165\u00a0<code>app.js<\/code>\u00a0\u6216\u8005\u4f60\u6240\u5e0c\u671b\u7684\u540d\u79f0\uff0c\u8fd9\u662f\u5f53\u524d\u5e94\u7528\u7684\u5165\u53e3\u6587\u4ef6\uff1b\u5982\u679c\u4f60\u5e0c\u671b\u91c7\u7528\u9ed8\u8ba4\u7684 index.js \u6587\u4ef6\u540d\uff0c\u53ea\u9700\u6309 \u201c<strong>\u56de\u8f66<\/strong>\u201d \u952e\u5373\u53ef\u3002<\/p>\n<h3 id=\"-express\">\u5b89\u88c5 Express<\/h3>\n<p>\u63a5\u4e0b\u6765\u5b89\u88c5 Express \u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u4f9d\u8d56\u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code>npm install express --save\r\n<\/code><\/pre>\n<p>\u5982\u679c\u53ea\u662f\u4e34\u65f6\u5b89\u88c5 Express\uff0c\u4e0d\u60f3\u5c06\u5b83\u6dfb\u52a0\u5230\u4f9d\u8d56\u5217\u8868\u4e2d\uff0c\u53ea\u9700\u7565\u53bb\u00a0<code>--save<\/code>\u00a0\u53c2\u6570\u5373\u53ef\u00a0<sup>[<a href=\"about:blank#stage-2-step-3-icon\">?<\/a>]<\/sup>\uff1a<\/p>\n<pre><code>npm install express\r\n<\/code><\/pre>\n<blockquote><p>\u5b89\u88c5 Node \u6a21\u5757\u65f6\uff0c\u5982\u679c\u6307\u5b9a\u4e86\u00a0<code>--save<\/code>\u00a0\u53c2\u6570\uff0c\u90a3\u4e48\u6b64\u6a21\u5757\u5c06\u88ab\u6dfb\u52a0\u5230\u00a0<code>package.json<\/code>\u00a0\u6587\u4ef6\u4e2d\u00a0<code>dependencies<\/code>\u00a0\u4f9d\u8d56\u5217\u8868\u4e2d\u3002 \u7136\u540e\u901a\u8fc7\u00a0<code>npm install<\/code>\u00a0\u547d\u4ee4\u5373\u53ef\u81ea\u52a8\u5b89\u88c5\u4f9d\u8d56\u5217\u8868\u4e2d\u6240\u5217\u51fa\u7684\u6240\u6709\u6a21\u5757\u3002<\/p><\/blockquote>\n<h2 id=\"hello-world\">Hello world<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min<\/p><\/blockquote>\n<h3 id=\"-app-js\">\u521b\u5efa app.js<\/h3>\n<p>\u5728 hello \u76ee\u5f55\u4e2d\uff0c<em>\u521b\u5efa app.js<\/em>\uff0c\u7136\u540e\u5c06\u4e0b\u5217\u4ee3\u7801\u590d\u5236\u8fdb\u53bb\uff1a<\/p>\n<h5 id=\"-data-release-hello-app-js\">\u793a\u4f8b\u4ee3\u7801\uff1a\/data\/release\/hello\/app.js<\/h5>\n<pre><code class=\"lang-javascript\">var express = require('express');\r\nvar app = express();\r\n\r\napp.get('\/', function (req, res) {\r\n  res.send('Hello World!');\r\n});\r\n\r\nvar server = app.listen(3000, function () {\r\n  console.log('Example app listening on port 3000!');\r\n});\r\n<\/code><\/pre>\n<p>\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u00a0<code>Ctrl + S<\/code>\u00a0\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<p><sup>[<a href=\"about:blank#stage-3-step-1-icon\">?<\/a>]<\/sup>\u00a0\u4e0a\u9762\u7684\u4ee3\u7801\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u5e76\u76d1\u542c\u4ece 3000 \u7aef\u53e3\u8fdb\u5165\u7684\u6240\u6709\u8fde\u63a5\u8bf7\u6c42\u3002\u4ed6\u5c06\u5bf9\u6240\u6709 (\/) URL \u6216\u00a0<strong>\u8def\u7531<\/strong>\u00a0\u8fd4\u56de \u201cHello World!\u201d \u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u5176\u4ed6\u6240\u6709\u8def\u5f84\u5168\u90e8\u8fd4\u56de\u00a0<strong>404 Not Found<\/strong>\u00a0\u3002<\/p>\n<blockquote><p>req (\u8bf7\u6c42) \u548c res (\u54cd\u5e94) \u4e0e Node \u63d0\u4f9b\u7684\u5bf9\u8c61\u5b8c\u5168\u4e00\u81f4\uff0c\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528 req.pipe()\u3001req.on(&#8216;data&#8217;, callback) \u4ee5\u53ca\u4efb\u4f55 Node \u63d0\u4f9b\u7684\u65b9\u6cd5\u3002<\/p><\/blockquote>\n<h3 id=\"-\">\u542f\u52a8\u5e94\u7528<\/h3>\n<p>\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u542f\u52a8\u6b64\u5e94\u7528\uff1a<\/p>\n<pre><code>node app.js\r\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00 http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000 \u5e76\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p>\uff08\u5982\u679c\u8bbf\u95ee\u4e0d\u6210\u529f\uff0c\u53ef\u80fd\u662f\u673a\u5668\u5b89\u5168\u7ec4\u7981\u7528\u4e86 3000 \u7aef\u53e3\u6240\u81f4\uff0c\u4f60\u53ef\u4ee5\u524d\u5f80<em>\u63a7\u5236\u53f0<\/em>\u4fee\u6539\u5b89\u5168\u7ec4\u914d\u7f6e\u3002\uff09<\/p>\n<p>\u8be5\u6b65\u9aa4\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u00a0<code>Ctrl + C<\/code>\u00a0\u7ec8\u6b62\u8fd0\u884c\u3002<\/p>\n<h2 id=\"express-\">Express \u5e94\u7528\u751f\u6210\u5668<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min<\/p><\/blockquote>\n<h3 id=\"-express-\">\u5b89\u88c5 Express \u5e94\u7528\u751f\u6210\u5668<\/h3>\n<p>\u901a\u8fc7\u5e94\u7528\u751f\u6210\u5668\u5de5\u5177 express \u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7684\u9aa8\u67b6\u3002<\/p>\n<p>\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<pre><code>npm install express-generator -g\r\n<\/code><\/pre>\n<p>-h \u9009\u9879\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u547d\u4ee4\u884c\u9009\u9879\uff1a<\/p>\n<pre><code>express -h\r\n<\/code><\/pre>\n<p>\u5c06\u5f97\u5230\u8f93\u51fa\uff1a<\/p>\n<pre><code>  Usage: express [options] [dir]\r\n\r\n  Options:\r\n\r\n    -h, --help          output usage information\r\n    -V, --version       output the version number\r\n    -e, --ejs           add ejs engine support (defaults to jade)\r\n        --hbs           add handlebars engine support\r\n    -H, --hogan         add hogan.js engine support\r\n    -c, --css &lt;engine&gt;  add stylesheet &lt;engine&gt; support (less|stylus|compass|sass) (defaults to plain css)\r\n        --git           add .gitignore\r\n    -f, --force         force on non-empty directory\r\n<\/code><\/pre>\n<h3 id=\"-\">\u521b\u5efa\u9879\u76ee<\/h3>\n<p>\u8fdb\u5165\u5de5\u4f5c\u76ee\u5f55\uff1a<\/p>\n<pre><code>cd \/data\/release\r\n<\/code><\/pre>\n<p>\u6267\u884c\u5982\u4e0b\u547d\u4ee4\uff0c\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u4e3a\u00a0<code>myapp<\/code>\u00a0\u7684\u5e94\u7528\uff1a<\/p>\n<pre><code>express myapp\r\n<\/code><\/pre>\n<p>\u5b8c\u6210\u540e\uff0c<em>\u70b9\u51fb\u67e5\u770b myapp \u9879\u76ee<\/em>\u3002<\/p>\n<p>\u751f\u6210\u7684\u5e94\u7528\u7a0b\u5e8f\u5177\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<pre><code>.\r\n\u251c\u2500\u2500 app.js\r\n\u251c\u2500\u2500 bin\r\n\u2502   \u2514\u2500\u2500 www\r\n\u251c\u2500\u2500 package.json\r\n\u251c\u2500\u2500 public\r\n\u2502   \u251c\u2500\u2500 images\r\n\u2502   \u251c\u2500\u2500 javascripts\r\n\u2502   \u2514\u2500\u2500 stylesheets\r\n\u2502       \u2514\u2500\u2500 style.css\r\n\u251c\u2500\u2500 routes\r\n\u2502   \u251c\u2500\u2500 index.js\r\n\u2502   \u2514\u2500\u2500 users.js\r\n\u2514\u2500\u2500 views\r\n    \u251c\u2500\u2500 error.pug\r\n    \u251c\u2500\u2500 index.pug\r\n    \u2514\u2500\u2500 layout.pug\r\n\r\n7 directories, 9 files\r\n<\/code><\/pre>\n<h3 id=\"-\">\u542f\u52a8\u5e94\u7528<\/h3>\n<p>\u8fdb\u5165\u8be5\u5e94\u7528\u76ee\u5f55\uff1a<\/p>\n<pre><code>cd myapp\r\n<\/code><\/pre>\n<p>\u7136\u540e\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u5305\uff1a<\/p>\n<pre><code>npm install\r\n<\/code><\/pre>\n<p>\u542f\u52a8\u8fd9\u4e2a\u5e94\u7528\uff08MacOS \u6216 Linux \u5e73\u53f0\uff09<sup>[<a href=\"about:blank#stage-4-step-3-icon\">?<\/a>]<\/sup>\uff1a<\/p>\n<pre><code>DEBUG=myapp npm start\r\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00 http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000 \u7f51\u5740\u5c31\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u5e94\u7528\u4e86\u3002<\/p>\n<p>\uff08\u8be5\u6b65\u9aa4\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u00a0<code>Ctrl + C<\/code>\u00a0\u7ec8\u6b62\u8fd0\u884c\u3002\uff09<\/p>\n<blockquote><p>Windows \u5e73\u53f0\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<code>set DEBUG=myapp &amp; npm start<\/code><\/p><\/blockquote>\n<h2 id=\"-\">\u57fa\u672c\u8def\u7531<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a10min ~ 15min<\/p><\/blockquote>\n<h3 id=\"express-\">Express \u8def\u7531\u7b80\u4ecb<\/h3>\n<p><strong>\u8def\u7531\uff08Routing\uff09<\/strong>\u662f\u7531\u4e00\u4e2a URI\uff08\u6216\u8005\u53eb\u8def\u5f84\uff09\u548c\u4e00\u4e2a\u7279\u5b9a\u7684 HTTP \u65b9\u6cd5\uff08GET\u3001POST \u7b49\uff09\u7ec4\u6210\u7684\uff0c\u6d89\u53ca\u5230\u5e94\u7528\u5982\u4f55\u54cd\u5e94\u5ba2\u6237\u7aef\u5bf9\u67d0\u4e2a\u7f51\u7ad9\u8282\u70b9\u7684\u8bbf\u95ee\u3002<\/p>\n<p>\u6bcf\u4e00\u4e2a\u8def\u7531\u90fd\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5904\u7406\u5668\u51fd\u6570\uff0c\u5f53\u5339\u914d\u5230\u8def\u7531\u65f6\uff0c\u8fd9\u4e9b\u51fd\u6570\u5c06\u88ab\u6267\u884c\u3002<\/p>\n<p>\u8def\u7531\u7684\u5b9a\u4e49\u7531\u5982\u4e0b\u7ed3\u6784\u7ec4\u6210\uff1a<\/p>\n<pre><code>app.METHOD(PATH, HANDLER)\r\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>app<\/code>\u00a0\u662f\u4e00\u4e2a express \u5b9e\u4f8b\uff1b<\/li>\n<li><code>METHOD<\/code>\u00a0\u662f\u67d0\u4e2a\u00a0<em>HTTP \u8bf7\u6c42\u65b9\u5f0f<\/em>\u00a0\u4e2d\u7684\u4e00\u4e2a<\/li>\n<li><code>PATH<\/code>\u00a0\u662f\u670d\u52a1\u5668\u7aef\u7684\u8def\u5f84\uff1b<\/li>\n<li><code>HANDLER<\/code>\u00a0\u662f\u5f53\u8def\u7531\u5339\u914d\u5230\u65f6\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<h3 id=\"-express-\">\u4e00\u4e2a\u7b80\u5355\u7684 Express \u8def\u7531<\/h3>\n<h4 id=\"-hello-\">\u4fee\u6539 hello \u9879\u76ee<\/h4>\n<p>\u8fd4\u56de\u5f00\u59cb\u521b\u5efa\u7684\u00a0<code>hello<\/code>\u00a0\u9879\u76ee\uff1a<\/p>\n<pre><code>cd \/data\/release\/hello\r\n<\/code><\/pre>\n<p><em>\u7f16\u8f91 app.js<\/em>\uff0c\u53c2\u8003\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"-data-release-hello-app-js\">\u793a\u4f8b\u4ee3\u7801\uff1a\/data\/release\/hello\/app.js<\/h5>\n<pre><code class=\"lang-javascript\">var express = require('express');\r\nvar app = express();\r\n\r\n\/\/ \u5bf9\u7f51\u7ad9\u9996\u9875\u7684\u8bbf\u95ee\u8fd4\u56de \"Hello World!\" \u5b57\u6837\r\napp.get('\/', function (req, res) {\r\n  res.send('Hello World!');\r\n});\r\n\r\n\/\/ \u7f51\u7ad9\u9996\u9875\u63a5\u53d7 POST \u8bf7\u6c42\r\napp.post('\/', function (req, res) {\r\n  res.send('Got a POST request');\r\n});\r\n\r\n\/\/ \/user \u8282\u70b9\u63a5\u53d7 PUT \u8bf7\u6c42\r\napp.put('\/user', function (req, res) {\r\n  res.send('Got a PUT request at \/user');\r\n});\r\n\r\n\/\/ \/user \u8282\u70b9\u63a5\u53d7 DELETE \u8bf7\u6c42\r\napp.delete('\/user', function (req, res) {\r\n  res.send('Got a DELETE request at \/user');\r\n});\r\n\r\nvar server = app.listen(3000, function () {\r\n  console.log('Example app listening on port 3000!');\r\n});\r\n<\/code><\/pre>\n<h4 id=\"-\">\u542f\u52a8\u5e94\u7528<\/h4>\n<pre><code>node app.js\r\n<\/code><\/pre>\n<p>\uff08\u8be5\u6b65\u9aa4\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u00a0<code>Ctrl + C<\/code>\u00a0\u7ec8\u6b62\u8fd0\u884c\u3002\uff09<\/p>\n<h4 id=\"-\">\u6d4b\u8bd5<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0<code>curl<\/code>\u00a0\u547d\u4ee4\u6216\u00a0<code>Postman<\/code>\u00a0\u7b49\u5de5\u5177\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<p>\u5982\u5728\u672c\u5730\u7ec8\u7aef\u6267\u884c\uff1a<\/p>\n<pre><code>curl -X POST http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\r\n<\/code><\/pre>\n<pre><code>curl -X PUT http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/user\r\n<\/code><\/pre>\n<pre><code>curl -X DELETE http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/user\r\n<\/code><\/pre>\n<h2 id=\"-\">\u9759\u6001\u6587\u4ef6<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min<\/p><\/blockquote>\n<h3 id=\"-express-\">\u5229\u7528 Express \u6258\u7ba1\u9759\u6001\u6587\u4ef6<\/h3>\n<p>\u901a\u8fc7 Express \u5185\u7f6e\u7684\u00a0<code>express.static<\/code>\u00a0\u53ef\u4ee5\u65b9\u4fbf\u5730\u6258\u7ba1\u9759\u6001\u6587\u4ef6\uff0c\u4f8b\u5982\u56fe\u7247\u3001CSS\u3001JavaScript \u6587\u4ef6\u7b49\u3002<\/p>\n<h4 id=\"-\">\u521b\u5efa\u9759\u6001\u76ee\u5f55<\/h4>\n<p>\u521b\u5efa public \u76ee\u5f55\uff1a<\/p>\n<pre><code>mkdir -p \/data\/release\/hello\/public\r\n<\/code><\/pre>\n<p>\u5728 public \u76ee\u5f55\u4e0b\uff0c<em>\u521b\u5efa hello.html<\/em>\uff0c\u7136\u540e\u590d\u5236\u4e0b\u5217\u4ee3\u7801\u5230\u00a0<strong>hello.html<\/strong>\u00a0\u4e2d\uff1a<\/p>\n<h5 id=\"-data-release-hello-public-hello-html\">\u793a\u4f8b\u4ee3\u7801\uff1a\/data\/release\/hello\/public\/hello.html<\/h5>\n<pre><code class=\"lang-html\">&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n  &lt;meta charset=\"UTF-8\"&gt;\r\n  &lt;title&gt;Document&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  &lt;h1&gt;Hello World&lt;\/h1&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<h4 id=\"-\">\u4fee\u6539\u5e94\u7528<\/h4>\n<p><em>\u7f16\u8f91 app.js<\/em>\uff0c\u53c2\u8003\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"-data-release-hello-app-js\">\u793a\u4f8b\u4ee3\u7801\uff1a\/data\/release\/hello\/app.js<\/h5>\n<pre><code class=\"lang-javascript\">var express = require('express');\r\nvar app = express();\r\n\r\napp.use(express.static('public'));\r\n\r\nvar server = app.listen(3000, function () {\r\n  console.log('Example app listening on port 3000!');\r\n});\r\n<\/code><\/pre>\n<p>\u6211\u4eec\u5728\u00a0<strong>app.js<\/strong>\u00a0\u4e2d\u5c06\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u00a0<code>express.static<\/code>\u00a0\u4e2d\u95f4\u4ef6\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u63d0\u4f9b\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u7684\u8bbf\u95ee\u4e86\u3002<\/p>\n<h4 id=\"-\">\u542f\u52a8\u5e94\u7528<\/h4>\n<pre><code>node app.js\r\n<\/code><\/pre>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00 http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/hello.html \u7f51\u5740\u5c31\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u6587\u4ef6\u4e86\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u5c06\u672c\u5730\u7684\u6587\u4ef6\u901a\u8fc7<strong>\u62d6\u62fd<\/strong>\u81f3\u5de6\u8fb9\u76ee\u5f55\u6811\u7684\u00a0<strong>public<\/strong>\u00a0\u76ee\u5f55\u4e0a\u4f20\u6587\u4ef6\u6765\u6d4b\u8bd5\u3002<\/p>\n<p>\u5047\u8bbe\u5728 public \u76ee\u5f55\u653e\u7f6e\u4e86\u56fe\u7247\u3001CSS \u548c JavaScript \u6587\u4ef6\uff0c\u4f60\u5c31\u53ef\u4ee5\u4ece\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\uff1a<\/p>\n<pre><code>http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/images\/kitten.jpg\r\nhttp:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/css\/style.css\r\nhttp:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/js\/app.js\r\nhttp:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/images\/bg.png\r\nhttp:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/hello.html\r\n<\/code><\/pre>\n<h3 id=\"static-\">static \u4e2d\u95f4\u4ef6\u66f4\u591a\u7528\u6cd5<\/h3>\n<h4 id=\"-\">\u591a\u4e2a\u76ee\u5f55<\/h4>\n<p>\u5982\u679c\u4f60\u7684\u9759\u6001\u8d44\u6e90\u5b58\u653e\u5728\u591a\u4e2a\u76ee\u5f55\u4e0b\u9762\uff0c\u4f60\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u00a0<code>express.static<\/code>\u00a0\u4e2d\u95f4\u4ef6\uff1a<\/p>\n<pre><code>app.use(express.static('public'));\r\napp.use(express.static('files'));\r\n<\/code><\/pre>\n<p>\u8bbf\u95ee\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u65f6\uff0c<code>express.static<\/code>\u00a0\u4e2d\u95f4\u4ef6\u4f1a\u6839\u636e\u76ee\u5f55\u6dfb\u52a0\u7684\u987a\u5e8f\u67e5\u627e\u6240\u9700\u7684\u6587\u4ef6\u3002<\/p>\n<h4 id=\"-\">\u6307\u5b9a\u8def\u5f84<\/h4>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u6240\u6709\u901a\u8fc7\u00a0<code>express.static<\/code>\u00a0\u8bbf\u95ee\u7684\u6587\u4ef6\u90fd\u5b58\u653e\u5728\u4e00\u4e2a\u201c\u865a\u62df\uff08virtual\uff09\u201d\u76ee\u5f55\uff08\u5373\u76ee\u5f55\u6839\u672c\u4e0d\u5b58\u5728\uff09\u4e0b\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e3a\u9759\u6001\u8d44\u6e90\u76ee\u5f55\u6307\u5b9a\u4e00\u4e2a\u6302\u8f7d\u8def\u5f84\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><em>\u7f16\u8f91 app.js<\/em>\uff0c\u53c2\u8003\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<h5 id=\"-data-release-hello-app-js\">\u793a\u4f8b\u4ee3\u7801\uff1a\/data\/release\/hello\/app.js<\/h5>\n<pre><code class=\"lang-javascript\">var express = require('express');\r\nvar app = express();\r\n\r\napp.use('\/static', express.static('public'));\r\n\r\nvar server = app.listen(3000, function () {\r\n  console.log('Example app listening on port 3000!');\r\n});\r\n<\/code><\/pre>\n<p>\u542f\u52a8\u5e94\u7528\uff1a<\/p>\n<pre><code>node app.js\r\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u5e26\u6709 \u201c\/static\u201d \u524d\u7f00\u7684\u5730\u5740\u6765\u8bbf\u95ee public \u76ee\u5f55\u4e0b\u9762\u7684\u6587\u4ef6\u4e86\u3002\u5982\uff1a<\/p>\n<p>http:\/\/&lt;\u60a8\u7684 CVM IP \u5730\u5740&gt;:3000\/static\/hello.html<\/p>\n<h2 id=\"-\">\u5b8c\u6210<\/h2>\n<blockquote><p>\u4efb\u52a1\u65f6\u95f4\uff1a1min<\/p><\/blockquote>\n<h3 id=\"-\">\u5b8c\u6210\u5b9e\u9a8c<\/h3>\n<p>\u606d\u559c\uff01\u60a8\u5df2\u7ecf\u5b8c\u6210\u4e86 Express \u5165\u95e8\u7684\u5168\u90e8\u5b9e\u9a8c\u5185\u5bb9\uff01<\/p>\n<p>\u66f4\u591a Express \u76f8\u5173\u8d44\u6599\uff0c\u8bf7\u67e5\u770b\u00a0<em>Express \u5b98\u7f51<\/em>\u00a0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Express \u5165\u95e8 \u5b89\u88c5 NodeJS \u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min \u5b89\u88c5 NodeJS \u5728\u7ec8\u7aef\u4e2d\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5b89\u88c5 NodeJS\uff1a curl &#8211;silent &#8211;location https:\/\/rpm.nodesource.com\/setup_8.x | sudo bash &#8211; yum -y install nodejs \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6d4b\u8bd5\u5b89\u88c5\u7ed3\u679c\uff1a node -v \u5b89\u88c5 Express \u4efb\u52a1\u65f6\u95f4\uff1a5min ~ 10min \u521b\u5efa\u5de5\u4f5c\u76ee\u5f55 \u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5728\u670d\u52a1\u5668\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u76ee\u5f55\uff1a mkdir -p \/data\/release\/hello \u8fdb\u5165\u6b64\u5de5\u4f5c\u76ee\u5f55\uff1a cd \/data\/release\/hello \u521d\u59cb\u5316\u9879\u76ee \u901a\u8fc7\u00a0npm init\u00a0\u547d\u4ee4\u4e3a\u4f60\u7684\u5e94\u7528\u521b\u5efa\u4e00\u4e2a\u00a0package.json\u00a0\u6587\u4ef6\u3002\u6b32\u4e86\u89e3\u00a0package.json\u00a0\u662f\u5982\u4f55\u8d77\u4f5c\u7528\u7684\uff0c\u8bf7\u53c2\u8003\u00a0Specifics of npm\u2019s package.json handling\u3002 npm init \u6b64\u547d\u4ee4\u5c06\u8981\u6c42\u4f60\u8f93\u5165\u51e0\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982\u6b64\u5e94\u7528\u7684\u540d\u79f0\u548c\u7248\u672c\u3002 \u9664\u00a0entry point: (index.js)\u00a0\u53c2\u6570\u5916\uff0c\u5176\u4ed6\u53c2\u6570\u4f60\u53ef\u4ee5\u76f4\u63a5\u6309 \u201c\u56de\u8f66\u201d \u952e\u63a5\u53d7\u9ed8\u8ba4\u8bbe\u7f6e\u5373\u53ef\u3002 \u5bf9\u4e8e\u00a0entry &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1442\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Express \u5165\u95e8<\/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":[228],"tags":[229,195],"class_list":["post-1442","post","type-post","status-publish","format-standard","hentry","category-node","tag-express","tag-node"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1442","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=1442"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1442\/revisions"}],"predecessor-version":[{"id":1443,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1442\/revisions\/1443"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}