{"id":24281,"date":"2024-11-21T13:58:40","date_gmt":"2024-11-21T05:58:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24281\/"},"modified":"2024-11-21T13:58:40","modified_gmt":"2024-11-21T05:58:40","slug":"%e6%9e%84%e5%bb%ba%e5%85%b7%e6%9c%89%e5%bc%ba%e5%a4%a7%e6%90%9c%e7%b4%a2%e5%8a%9f%e8%83%bd%e7%9a%84%e7%94%b5%e5%bd%b1%e7%bd%91%e7%ab%99%ef%bc%9awebman%e7%9a%84%e7%94%b5%e5%bd%b1%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24281\/","title":{"rendered":"\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169181075340917.jpg\" class=\"aligncenter\" title=\"\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357\u63d2\u56fe\" alt=\"\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p>\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357<\/p>\n<p>\u5bfc\u8bed\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u548c\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u4e60\u60ef\u4f7f\u7528\u7535\u5f71\u7f51\u7ad9\u6765\u6d4f\u89c8\u548c\u9009\u62e9\u7535\u5f71\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\u2014\u2014Webman\uff0c\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<p>\u4e00\u3001\u9879\u76ee\u51c6\u5907<\/p>\n<p>\u4e3a\u4e86\u6784\u5efaWebman\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u51c6\u5907\u4ee5\u4e0b\u5de5\u5177\u548c\u8d44\u6e90\uff1a<\/p>\n<ol>\n<li>Node.js\uff1a\u4e00\u79cd\u57fa\u4e8eChrome V8\u5f15\u64ce\u7684JavaScript\u8fd0\u884c\u73af\u5883\uff0c\u7528\u4e8e\u540e\u7aef\u5f00\u53d1\u3002<\/li>\n<li>Express.js\uff1a\u4e00\u4e2a\u5feb\u901f\u3001\u5f00\u653e\u3001\u6781\u7b80\u7684web\u5e94\u7528\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\u7aef\u5e94\u7528\u3002<\/li>\n<li>MongoDB\uff1a\u4e00\u79cd\u975e\u5173\u7cfb\u578b\u7684\u6570\u636e\u5e93\uff0c\u7528\u4e8e\u5b58\u50a8\u7535\u5f71\u4fe1\u606f\u6570\u636e\u3002<\/li>\n<li>TMDb API\uff1a\u4e00\u4e2a\u63d0\u4f9b\u7535\u5f71\u4fe1\u606f\u548c\u7535\u5f71\u6570\u636e\u5e93\u7684API\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u8be5API\u83b7\u53d6\u7535\u5f71\u6570\u636e\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u521b\u5efa\u9879\u76ee<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5728\u7535\u8111\u4e0a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aWebman\u7684\u6587\u4ef6\u5939\uff0c\u7136\u540e\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8fdb\u5165\u8be5\u6587\u4ef6\u5939\uff0c\u5e76\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684Node.js\u9879\u76ee\uff1a<\/p>\n<pre>npm init -y<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5b89\u88c5Express.js\u548cMongoDB\u7684Node.js\u9a71\u52a8\u7a0b\u5e8f\uff1a<\/p>\n<pre>npm install express mongodb<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff1a<\/p>\n<pre>const express = require('express');\nconst MongoClient = require('mongodb').MongoClient;\n\nconst app = express();\nconst port = 3000;\n\n\/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u4ee3\u7801<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u83b7\u53d6\u7535\u5f71\u6570\u636e<\/p>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528TMDb API\u6765\u83b7\u53d6\u7535\u5f71\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728TMDb\u5b98\u7f51\u4e0a\u7533\u8bf7\u4e00\u4e2aAPI\u5bc6\u94a5\u3002\u83b7\u53d6\u5230API\u5bc6\u94a5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5411TMDb\u53d1\u9001\u8bf7\u6c42\u5e76\u83b7\u53d6\u7535\u5f71\u6570\u636e\uff1a<\/p>\n<pre>const apiKey = 'YOUR_API_KEY';\n\napp.get('\/movies', (req, res) =&amp;gt; {\n  const url = `https:\/\/api.themoviedb.org\/3\/discover\/movie?api_key=${apiKey}`;\n\n  \/\/ \u4f7f\u7528Node.js\u7684\u5185\u7f6ehttp\u6a21\u5757\u6765\u53d1\u9001\u8bf7\u6c42\n  http.get(url, (response) =&amp;gt; {\n    let data = '';\n\n    response.on('data', (chunk) =&amp;gt; {\n      data += chunk;\n    });\n\n    response.on('end', () =&amp;gt; {\n      const movies = JSON.parse(data).results;\n      res.send(movies);\n    });\n  });\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u5b58\u50a8\u7535\u5f71\u6570\u636e<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5c06\u83b7\u53d6\u5230\u7684\u7535\u5f71\u6570\u636e\u5b58\u50a8\u5230MongoDB\u6570\u636e\u5e93\u4e2d\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728MongoDB\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3awebman\u7684\u6570\u636e\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amovies\u7684\u96c6\u5408\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<pre>const url = 'mongodb:\/\/localhost:27017';\nconst dbName = 'webman';\n\nMongoClient.connect(url, (err, client) =&amp;gt; {\n  if (err) {\n    console.log(err);\n  } else {\n    console.log('Connected to MongoDB');\n\n    const db = client.db(dbName);\n    const collection = db.collection('movies');\n    \n    app.get('\/movies\/save', (req, res) =&amp;gt; {\n      const url = `https:\/\/api.themoviedb.org\/3\/discover\/movie?api_key=${apiKey}`;\n\n      http.get(url, (response) =&amp;gt; {\n        let data = '';\n\n        response.on('data', (chunk) =&amp;gt; {\n          data += chunk;\n        });\n\n        response.on('end', () =&amp;gt; {\n          const movies = JSON.parse(data).results;\n          \n          collection.insertMany(movies, (err, result) =&amp;gt; {\n            if (err) {\n              console.log(err);\n              res.send(err);\n            } else {\n              console.log('Movies saved to database');\n              res.send('Movies saved to database');\n            }\n          });\n        });\n      });\n    });\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e94\u3001\u5b9e\u73b0\u641c\u7d22\u529f\u80fd<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u4e3aWebman\u6dfb\u52a0\u641c\u7d22\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u641c\u7d22\u63a5\u53e3\uff1a<\/p>\n<pre>app.get('\/movies\/search', (req, res) =&amp;gt; {\n  const keyword = req.query.keyword;\n\n  const query = {\n    $or: [\n      { title: { $regex: keyword, $options: 'i' } },\n      { overview: { $regex: keyword, $options: 'i' } }\n    ]\n  };\n\n  collection.find(query).toArray((err, result) =&amp;gt; {\n    if (err) {\n      console.log(err);\n      res.send(err);\n    } else {\n      res.send(result);\n    }\n  });\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\/movies\/search?keyword=\u5173\u952e\u8bcd\u6765\u5b9e\u73b0\u641c\u7d22\u529f\u80fd\u4e86\u3002<\/p>\n<p>\u516d\u3001\u6d4b\u8bd5<\/p>\n<p>\u81f3\u6b64\uff0cWebman\u7684\u57fa\u672c\u529f\u80fd\u5df2\u7ecf\u5b9e\u73b0\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u670d\u52a1\u5668\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:3000\/movies\/search?keyword=\u91d1\u521a\u72fc\u6765\u6d4b\u8bd5\u641c\u7d22\u529f\u80fd\uff1a<\/p>\n<pre>node index.js<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5c06\u770b\u5230\u8fd4\u56de\u7684\u7535\u5f71\u6570\u636e\u3002<\/p>\n<p>\u7ed3\u8bed\uff1a\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\u2014\u2014Webman\u3002\u901a\u8fc7\u4f7f\u7528Node.js\u3001Express.js\u548cMongoDB\uff0c\u6211\u4eec\u6210\u529f\u5b9e\u73b0\u4e86\u7535\u5f71\u6570\u636e\u7684\u83b7\u53d6\u3001\u5b58\u50a8\u548c\u641c\u7d22\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6784\u5efa\u81ea\u5df1\u7684\u7535\u5f71\u7f51\u7ad9\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6784\u5efa\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\uff1aWebman\u7684\u7535\u5f71\u5e94\u7528\u6307\u5357 \u5bfc\u8bed\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u548c\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u4e60\u60ef\u4f7f\u7528\u7535\u5f71\u7f51\u7ad9\u6765\u6d4f\u89c8\u548c\u9009\u62e9\u7535\u5f71\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u5f3a\u5927\u641c\u7d22\u529f\u80fd\u7684\u7535\u5f71\u7f51\u7ad9\u2014\u2014Webman\uff0c\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002 \u4e00\u3001\u9879\u76ee\u51c6\u5907 \u4e3a\u4e86\u6784\u5efaWebman\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u51c6\u5907\u4ee5\u4e0b\u5de5\u5177\u548c\u8d44\u6e90\uff1a Node.js\uff1a\u4e00\u79cd\u57fa\u4e8eChrome V8\u5f15\u64ce\u7684JavaScript\u8fd0\u884c\u73af\u5883\uff0c\u7528\u4e8e\u540e\u7aef\u5f00\u53d1\u3002 Express.js\uff1a\u4e00\u4e2a\u5feb\u901f\u3001\u5f00\u653e\u3001\u6781\u7b80\u7684web\u5e94\u7528\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u642d\u5efa\u670d\u52a1\u5668\u7aef\u5e94\u7528\u3002 MongoDB\uff1a\u4e00\u79cd\u975e\u5173\u7cfb\u578b\u7684\u6570\u636e\u5e93\uff0c\u7528\u4e8e\u5b58\u50a8\u7535\u5f71\u4fe1\u606f\u6570\u636e\u3002 TMDb API\uff1a\u4e00\u4e2a\u63d0\u4f9b\u7535\u5f71\u4fe1\u606f\u548c\u7535\u5f71\u6570\u636e\u5e93\u7684API\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u8be5API\u83b7\u53d6\u7535\u5f71\u6570\u636e\u3002 \u4e8c\u3001\u521b\u5efa\u9879\u76ee \u9996\u5148\uff0c\u6211\u4eec\u5728\u7535\u8111\u4e0a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aWebman\u7684\u6587\u4ef6\u5939\uff0c\u7136\u540e\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8fdb\u5165\u8be5\u6587\u4ef6\u5939\uff0c\u5e76\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684Node.js\u9879\u76ee\uff1a npm init -y \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u6211\u4eec\u5b89\u88c5Express.js\u548cMongoDB\u7684Node.js\u9a71\u52a8\u7a0b\u5e8f\uff1a npm install express mongodb \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff1a const express = require(&#8216;express&#8217;); const MongoClient = require(&#8216;mongodb&#8217;).MongoClient; const app = express(); const port = 3000; \/\/ \u5728\u8fd9\u91cc\u7f16\u5199\u4ee3\u7801 \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u83b7\u53d6\u7535\u5f71\u6570\u636e \u6211\u4eec\u5c06\u4f7f\u7528TMDb API\u6765\u83b7\u53d6\u7535\u5f71\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728TMDb\u5b98\u7f51\u4e0a\u7533\u8bf7\u4e00\u4e2aAPI\u5bc6\u94a5\u3002\u83b7\u53d6\u5230API\u5bc6\u94a5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5411TMDb\u53d1\u9001\u8bf7\u6c42\u5e76\u83b7\u53d6\u7535\u5f71\u6570\u636e\uff1a const apiKey = &#8216;YOUR_API_KEY&#8217;; app.get(&#8216;\/movies&#8217;, (req, res) =&amp;gt; { const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24281","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24281"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24281\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}