{"id":162585,"date":"2024-11-04T16:19:57","date_gmt":"2024-11-04T08:19:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/162585\/"},"modified":"2024-11-04T16:19:58","modified_gmt":"2024-11-04T08:19:58","slug":"%e7%b0%a1%e6%98%93%e9%ab%98%e6%95%88%e5%9f%ba%e6%96%bc-redis-%e7%9a%84-http-%e6%8e%a5%e5%8f%a3%e4%bd%bf%e7%94%a8%ef%bc%88redis-%e7%9a%84-http-%e6%8e%a5%e5%8f%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/162585\/","title":{"rendered":"\u7c21\u6613\u9ad8\u6548\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\u4f7f\u7528\uff08redis \u7684 http \u63a5\u53e3\uff09"},"content":{"rendered":"<h1 id=\"%e7%b0%a1%e6%98%93%e9%ab%98%e6%95%88%e5%9f%ba%e6%96%bc-redis-%e7%9a%84-http-%e6%8e%a5%e5%8f%a3%e4%bd%bf%e7%94%a8%ef%bc%88redis-%e7%9a%84-http-%e6%8e%a5%e5%8f%a3%ef%bc%89-lnxabBdiYh\">\u7c21\u6613\u9ad8\u6548\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\u4f7f\u7528\uff08redis \u7684 http \u63a5\u53e3\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u7684\u5feb\u901f\u5b58\u53d6\u548c\u9ad8\u6548\u8655\u7406\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u69cb\u5efa\u4e00\u500b\u7c21\u6613\u9ad8\u6548\u7684\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-lnxabBdiYh\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis\uff08REmote DIctionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u5b83\u7684\u9ad8\u6027\u80fd\u7279\u6027\u4f7f\u5176\u6210\u70ba\u8a31\u591a\u61c9\u7528\u7684\u9996\u9078\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u7684\u5834\u666f\u4e2d\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8-http-%e6%8e%a5%e5%8f%a3%ef%bc%9f-lnxabBdiYh\">\u70ba\u4ec0\u9ebc\u4f7f\u7528 HTTP \u63a5\u53e3\uff1f<\/h2>\n<p>HTTP \u63a5\u53e3\u4f7f\u5f97\u4e0d\u540c\u7684\u61c9\u7528\u7a0b\u5e8f\u80fd\u5920\u901a\u904e\u7db2\u7d61\u9032\u884c\u901a\u4fe1\u3002\u901a\u904e\u5c07 Redis \u8207 HTTP \u63a5\u53e3\u7d50\u5408\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07 Redis \u7684\u6578\u64da\u5b58\u53d6\u529f\u80fd\u66b4\u9732\u7d66\u5176\u4ed6\u61c9\u7528\uff0c\u7121\u8ad6\u662f\u524d\u7aef\u61c9\u7528\u9084\u662f\u5176\u4ed6\u5f8c\u7aef\u670d\u52d9\u3002<\/p>\n<h2 id=\"%e6%a7%8b%e5%bb%ba%e5%9f%ba%e6%96%bc-redis-%e7%9a%84-http-%e6%8e%a5%e5%8f%a3-lnxabBdiYh\">\u69cb\u5efa\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3<\/h2>\n<p>\u4ee5\u4e0b\u662f\u69cb\u5efa\u4e00\u500b\u7c21\u55ae\u7684\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-lnxabBdiYh\">1. \u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86 Redis \u548c Node.js\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Redis\uff1a<\/p>\n<pre><code>sudo apt-get install redis-server<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u5b89\u88dd Express \u6846\u67b6\u4f86\u69cb\u5efa HTTP \u670d\u52d9\uff1a<\/p>\n<pre><code>npm install express redis<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba-http-%e6%9c%8d%e5%8b%99-lnxabBdiYh\">2. \u5275\u5efa HTTP \u670d\u52d9<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Node.js \u670d\u52d9\u5668\u793a\u4f8b\uff0c\u8a72\u670d\u52d9\u5668\u4f7f\u7528 Express \u6846\u67b6\u4f86\u8655\u7406 HTTP \u8acb\u6c42\uff0c\u4e26\u8207 Redis \u9032\u884c\u4ea4\u4e92\uff1a<\/p>\n<pre><code>const express = require('express');\nconst redis = require('redis');\n\nconst app = express();\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Redis error: ' + err);\n});\n\napp.use(express.json());\n\napp.post('\/set', (req, res) =&gt; {\n    const { key, value } = req.body;\n    client.set(key, value, (err, reply) =&gt; {\n        if (err) {\n            return res.status(500).send(err);\n        }\n        res.send(`Key ${key} set with value ${value}`);\n    });\n});\n\napp.get('\/get\/:key', (req, res) =&gt; {\n    const key = req.params.key;\n    client.get(key, (err, reply) =&gt; {\n        if (err) {\n            return res.status(500).send(err);\n        }\n        res.send(`Value for key ${key} is ${reply}`);\n    });\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () =&gt; {\n    console.log(`Server is running on port ${PORT}`);\n});<\/code><\/pre>\n<h3 id=\"3-%e6%b8%ac%e8%a9%a6-http-%e6%8e%a5%e5%8f%a3-lnxabBdiYh\">3. \u6e2c\u8a66 HTTP \u63a5\u53e3<\/h3>\n<p>\u555f\u52d5\u670d\u52d9\u5668\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 Postman \u6216 curl \u4f86\u6e2c\u8a66\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8a2d\u7f6e\u548c\u7372\u53d6\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<ul>\n<li>\u8a2d\u7f6e\u6578\u64da\uff1a<\/li>\n<pre><code>curl -X POST http:\/\/localhost:3000\/set -H \"Content-Type: application\/json\" -d '{\"key\":\"name\", \"value\":\"Server.HK\"}'<\/code><\/pre>\n<li>\u7372\u53d6\u6578\u64da\uff1a<\/li>\n<pre><code>curl http:\/\/localhost:3000\/get\/name<\/code><\/pre>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-lnxabBdiYh\">\u7e3d\u7d50<\/h2>\n<p>\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u5b58\u53d6\u548c\u7ba1\u7406\u6578\u64da\u3002\u901a\u904e\u4f7f\u7528 Node.js \u548c Express \u6846\u67b6\uff0c\u6211\u5011\u53ef\u4ee5\u5feb\u901f\u69cb\u5efa\u4e00\u500b\u80fd\u5920\u8207 Redis \u9032\u884c\u4ea4\u4e92\u7684 HTTP \u670d\u52d9\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u5b58\u53d6\u7684\u6548\u7387\uff0c\u9084\u4f7f\u5f97\u4e0d\u540c\u61c9\u7528\u4e4b\u9593\u7684\u6578\u64da\u5171\u4eab\u8b8a\u5f97\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u4f60\u7684\u61c9\u7528\uff0cServer.HK \u63d0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u7c21\u6613\u9ad8\u6548\u7684\u57fa\u65bc Redis \u7684 HTTP \u63a5\u53e3\u4f7f\u7528\uff0c\u63d0\u5347\u6578\u64da\u5b58\u53d6\u6548\u7387\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\uff0c\u5be6\u73fe\u5feb\u901f\u97ff\u61c9\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-162585","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=162585"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162585\/revisions"}],"predecessor-version":[{"id":162586,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162585\/revisions\/162586"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=162585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=162585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=162585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}