我可以从浏览器中运行的 JavaScript 直接连接到 Redis 服务器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5759120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I connect directly to a Redis server from JavaScript running in a browser?
提问by AlexChaffee
I know there are node.js libraries for Redis; what I'd like to do is run a Redis server (either on localhost or on a server host somewhere) and call it directly via HTTP (i.e. AJAX or HTTP GET as needed) from JavaScript running inside a browser (i.e. a Greasemonkey or Chrome Extension script, or maybe a bookmarklet or SCRIPT tag). Does Redis have a native REST or HTTP API?
我知道 Redis 有 node.js 库;我想要做的是运行 Redis 服务器(在本地主机上或某处的服务器主机上),并从浏览器中运行的 JavaScript(即 Greasemonkey 或 Chrome)通过 HTTP(即 AJAX 或 HTTP GET,根据需要)直接调用它扩展脚本,或者可能是书签或 SCRIPT 标签)。Redis 有原生的 REST 或 HTTP API 吗?
回答by Theo
You can't connect directly to Redis from JavaScript running in a browser because Redis does not speak HTTP. What you can do is put webdisin front of Redis, it makes it possible work with a Redis instance over a HTTP interface.
您无法从浏览器中运行的 JavaScript 直接连接到 Redis,因为 Redis 不支持 HTTP。您可以做的是将webdis放在 Redis 前面,它可以通过 HTTP 接口使用 Redis 实例。
回答by Ram
Webdis is very very slow as compared to using an NGINX server in front of redis
与在 redis 前面使用 NGINX 服务器相比,Webdis 非常非常慢
If you just implement a simple redis client in mod-perl and expose behind nginx, you can easily get very good performance. And you can handle a lot of logic too
如果你只是在 mod-perl 中实现一个简单的 redis 客户端并暴露在 nginx 后面,你可以很容易地获得非常好的性能。你也可以处理很多逻辑
回答by eyeApps LLC
You can literally connect to the redis server over http, and there's a security exploit based on this.
你可以直接通过 http 连接到 redis 服务器,并且有一个基于此的安全漏洞。
Redis is effectively a HTTP server -- http://benmmurphy.github.io/blog/2015/06/04/redis-eval-lua-sandbox-escape/
Redis 实际上是一个 HTTP 服务器——http://benmmurphy.github.io/blog/2015/06/04/redis-eval-lua-sandbox-escape/
Maybe this could be used to make a javascript client for redis? In the examples shown, commands are sent directly to the redis server, which executes them. However, practically speaking, you can use openresty+nginx in front of redis to essentially directly talk to redis over http, and get the performance benefit of nginx's multithreaded server which will share a limited set of connections to redis itself.
也许这可以用来为redis制作一个javascript客户端?在显示的示例中,命令直接发送到 redis 服务器,redis 服务器执行它们。但是,实际上,您可以在 redis 前面使用 openresty+nginx 基本上直接通过 http 与 redis 对话,并获得 nginx 的多线程服务器的性能优势,该服务器将共享一组有限的连接到 redis 本身。
回答by Paul
As @Theo explained, you can't connect directly, but if you have webdis and redis set up then I have a library that eliminates mucking around with ajax yourself, in favor of a promises based approach.
正如@Theo 解释的那样,您无法直接连接,但是如果您设置了 webdis 和 redis,那么我有一个库可以消除自己使用 ajax 的麻烦,支持基于承诺的方法。
webdismayis a JS library I have recently released (License: MIT) to connect to a webdis+redis backend from the browser. It takes an ES6 Promises approach to communicating with the redis+webdis back end, providing functionsfor generic and keyless redis commands and organized classes for commands that operate on Keys/Strings, Lists, Hashand Sets. All functions/methods return ES6 Promises.
webdismay是我最近发布的一个 JS 库(许可证:MIT),用于从浏览器连接到 webdis+redis 后端。它采用 ES6 Promises 方法与 redis+webdis 后端通信,为通用和无键 redis 命令提供函数,并为对Keys/Strings、Lists、Hash和Sets操作的命令提供组织类。所有函数/方法都返回 ES6 Promises。
Assuming you have webdis set up with redis, in the default configuration to accept post requests to "/", then with webdismay a simple example of sending data to the server and getting it back later would look like this on the browser (in ES6):
假设您使用 redis 设置了 webdis,在默认配置中接受对“/”的 post 请求,然后使用 webdismay 向服务器发送数据并稍后将其取回的简单示例在浏览器上看起来像这样(在 ES6 中) :
import 'whatwg-fetch'; // fetch polyfill
import * as W from 'webdismay';
const k = W.key('some-redis-key');
k.set('Hello, World!'); // store the information
// wait a while, e.g. in the dev console or with setTimeout()
k.get().then((v)=>console.log(v)); // --> Hello, World!
At the time I am writing this (July 2016), the first two import
lines require some translation and packaging assistance from tools like jspm or browserify (if you change the import
to require
).
在我撰写本文时(2016 年 7 月),前两import
行需要来自 jspm 或 browserify 等工具的一些翻译和打包帮助(如果您将 更改import
为require
)。
This is not exactly beginner-friendly, yet, but could allow someone to use webdis+redis from the browser without constantly translating mentally between javascript idioms and redis and writing their own ajax.
这并不完全适合初学者,但可以允许某人从浏览器使用 webdis+redis,而无需不断在 javascript 习语和 redis 之间进行心理转换并编写自己的 ajax。