{"id":199,"date":"2021-02-10T10:09:51","date_gmt":"2021-02-10T02:09:51","guid":{"rendered":"http:\/\/gjweb.top\/?p=199"},"modified":"2021-02-10T10:09:51","modified_gmt":"2021-02-10T02:09:51","slug":"%e4%b8%ad%e9%97%b4%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=199","title":{"rendered":"\u4e2d\u95f4\u4ef6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4f5c\u7528<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u6267\u884c\u4efb\u4f55\u4ee3\u7801<\/li><li>\u4fee\u6539\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61<\/li><li>\u7ec8\u7ed3\u8bf7\u6c42-\u54cd\u5e94\u5faa\u73af. (\u8ba9\u4e00\u6b21\u8bf7\u6c42\u5f97\u5230\u54cd\u5e94)<\/li><li>\u8c03\u7528\u5806\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u8005\u8def\u7531<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5206\u7c7b<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*<br>    1. \u5e94\u7528(\u5168\u5c40)\u7ea7\u4e2d\u95f4\u4ef6(\u8fc7\u6ee4\u975e\u6cd5\u7684\u8bf7\u6c42,\u4f8b\u5982\u9632\u76d7\u94fe)<br>        \u7b2c\u4e00\u79cd: app.use((request,response,next)=&gt;{}) \/\/ \u6240\u6709\u8bf7\u6c42\u90fd\u9700\u8981\u7ecf\u8fc7\u67d0\u4e9b\u5904\u7406\u7684\u65f6\u5019\u7528\u6b64\u79cd\u5199\u6cd5<br>        \u7b2c\u4e8c\u79cd: \u4f7f\u7528\u51fd\u6570\u5b9a\u4e49<br>    2. \u7b2c\u4e09\u65b9\u4e2d\u95f4\u4ef6(\u4f8b\u5982: body-parser)<br> &nbsp;      app.use(hodyParser.urlencoded({extebded:true}))<br> &nbsp;  3. \u5185\u7f6e\u4e2d\u95f4\u4ef6(express\u5185\u90e8\u5c01\u88c5\u597d\u7684\u4e2d\u95f4\u4ef6)<br> &nbsp;      app.use(express.urlencoded({extended:true}))<br> &nbsp;      app.use(express.static('public'))<br> &nbsp;  4. \u8def\u7531\u5668\u4e2d\u95f4\u4ef6(Router)<br> &nbsp; &nbsp;<br> \u5907\u6ce8: <br>    1. \u5728exoress\u4e2d, \u5b9a\u4e49\u8def\u7531\u548c\u4e2d\u95f4\u4ef6\u7684\u65f6\u5019,\u6839\u636e\u5b9a\u4e49\u7684\u987a\u5e8f(\u4ee3\u7801\u7684\u987a\u5e8f),\u5c06\u5b9a\u4e49\u7684\u6bcf\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u8def\u7531,\u653e\u5728\u4e00\u4e2a\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u7684\u5bb9\u5668\u4e2d,\u5f53\u8bf7\u6c42\u8fc7\u6765\u7684\u65f6\u5019,\u4e00\u6b21\u4ece\u5bb9\u5668\u4e2d\u53d6\u51fa\u4e2d\u95f4\u4ef6\u548c\u8def\u7531,\u8fdb\u884c\u5339\u914d,\u5982\u679c\u5339\u914d\u6210\u529f,\u4ea4\u7531\u8be5\u8def\u7531\u6216\u4e2d\u95f4\u4ef6\u5904\u7406,\u5982\u679c\u5168\u5c40\u4e2d\u95f4\u4ef6\u5199\u5728\u4e86\u6700\u5f00\u59cb\u7684\u4f4d\u7f6e,\u90a3\u4e48\u4ed6\u5c31\u662f\u8bf7\u6c42\u7684\"\u7b2c\u4e00\u6247\u95e8\"<br>    2. \u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bf4,\u7b2c\u4e00\u6b21\u8bf7\u6c42,\u53ea\u6709\u4e00\u4e2a\u8bf7\u6c42\u5bf9\u8c61,\u548c\u4e00\u4e2a\u76f8\u5e94\u5bf9\u8c61,\u5176\u4ed6\u4efb\u4f55\u7684request\u548cresponse\u90fd\u662f\u5bf9\u4e8c\u8005\u7684\u5f15\u7528<br>*\/<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e2d\u95f4\u4ef6\u5b9e\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528app.use\u521b\u5efa\u5168\u5c40\u4e2d\u95f4\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">const express = require(\"express\")<br>const app = express();<br>\/\/ \u4f7f\u7528\u5168\u5c40\u4e2d\u95f4\u4ef6----\u6240\u7528\u8bf7\u6c42\u7684\u7b2c\u4e00\u6247\u95e8<br>app.use((request,response,next)=&gt;{<br> &nbsp; &nbsp;\/**<br> &nbsp;  * 1. \u8fc7\u6ee4\u4e00\u4e9b\u4e0d\u5408\u6cd5\u7684\u8bf7\u6c42\uff0c\u4f8b\u5982\u9632\u76d7\u94fe<br> &nbsp;  *\/<br> &nbsp; &nbsp;if(request.get('Referer')){<br> &nbsp; &nbsp; &nbsp; &nbsp;let miniReferer = request.get(\"Referer\").split(\"\/\")[2]<br> &nbsp; &nbsp; &nbsp; &nbsp;if(miniReferer === \"localhost:8081\"){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next()<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53d1\u751f\u4e86\u76d7\u94fe<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.send(\"\u8bf7\u5728\u672c\u7ad9\u4f7f\u7528\u8d44\u6e90\")<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;next()<br> &nbsp;  }<br>})<br>\u200b<br>app.get(\"\/\",(request,response)=&gt;{<br> &nbsp; &nbsp;response.send(\"success\")<br>})<br>app.listen(3000,(err)=&gt;{<br> &nbsp; &nbsp;if(err){<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"server error\")<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;console.log(\"server success \")<br> &nbsp;  }<br>})<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u51fd\u6570\u65b9\u5f0f\u521b\u5efa\u5168\u5c40\u4e2d\u95f4\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">function demo (request,response,next){<br> &nbsp; &nbsp;if(request.get('Referer')){<br> &nbsp; &nbsp; &nbsp; &nbsp;let miniReferer = request.get(\"Referer\").split(\"\/\")[2]<br> &nbsp; &nbsp; &nbsp; &nbsp;if(miniReferer === \"localhost:8081\"){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next()<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53d1\u751f\u4e86\u76d7\u94fe<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response.send(\"\u8bf7\u5728\u672c\u7ad9\u4f7f\u7528\u8d44\u6e90\")<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp;next()<br> &nbsp;  }<br>}<br>\/\/ \u8c01\u8c03\u7528,\u4f5c\u7528\u5728\u54ea\u4e2a\u8def\u7531\u4e0b<br>app.get(\"\/\", demo ,(request,response)=&gt;{<br> &nbsp; &nbsp;response.send(\"success\")<br>})<br>\u200b<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528body-parser\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i body-parser<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">const express = require(\"express\")<br>\/\/ \u5f15\u5165body-parser \u7528\u4e8e\u89e3\u6790post\u53c2\u6570<br>const bodyParser = require(\"body-parser\")<br>const app = express();<br>\/\/ \u4f7f\u7528\u7b2c\u4e09\u65b9\u4e2d\u95f4\u4ef6<br>app.use(bodyParser.urlencoded({extended:true})) \/\/ \u89e3\u6790post\u8bf7\u6c42\u8bf7\u6c42\u4f53\u4e2d\u6240\u643a\u5e26\u7684urllencided\u7f16\u7801\u5f62\u5f0f\u7684\u53c2\u6570\u4e3a\u4e00\u4e2a\u5bf9\u8c61,\u6302\u8f7d\u5230request\u5bf9\u8c61\u4e0a<br>app.post(\"\/test\",(request,response)=&gt;{<br> &nbsp; &nbsp;console.log(request.body)<br>})<br>\u200b<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u5185\u7f6e\u4e2d\u95f4\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">const express = require(\"express\")<br>const app = express();<br>\/\/ \u4f7f\u7528\u7b2c\u4e09\u65b9\u4e2d\u95f4\u4ef6<br>app.use(express.urlencoded({extended:true}))<br>\/\/ \u4f7f\u7528\u5185\u7f6e\u4e2d\u95f4\u4ef6\u53bb\u66b4\u9732\u9759\u6001\u8d44\u6e90(js,css,html,img\u7b49\u6587\u4ef6)<br>\/*<br>    \u66b4\u9732\u9759\u6001\u8d44\u6e90\u7684\u5f0a\u7aef: \u8bbf\u95ee\u662f\u5fc5\u987b\u5e26\u4e0a\u6587\u4ef6\u7684\u6269\u5c55\u540d<br>        \u5982 www.xxx.com\/index.html  200<br>          www.xxx.com\/index 404<br>            <br>*\/<br>app.use(express.static(__dirname+'\/public'))<br>app.post(\"\/test\",(request,response)=&gt;{<br> &nbsp; &nbsp;console.log(request.body)<br>})<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u7528 \u6267\u884c\u4efb\u4f55\u4ee3\u7801 \u4fee\u6539\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61 \u7ec8\u7ed3\u8bf7\u6c42-\u54cd\u5e94\u5faa\u73af. (\u8ba9\u4e00\u6b21\u8bf7\u6c42\u5f97\u5230\u54cd\u5e94) \u8c03\u7528\u5806\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-express"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/199\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}