没有服务器端的 CouchApp 或有 xdomain 问题的 CouchDB 后端?

发布于 2024-10-01 18:39:15 字数 550 浏览 4 评论 0原文

我已经使用 CouchDB 和 CouchApp 一段时间了。我计划将它用于我正在开发的一个新网站项目。

  1. 从可扩展性的角度来看,我喜欢 CouchApp 的想法。

    缺点是,由于没有服务器端代码,有些事情(例如 oAuth 身份验证)很难在客户端完成。在某些时候,我确信我将需要一些具有某种描述的服务器端代码 - 我想您可以查看 Node.js,但此时不希望这样做。

  2. 使用 CouchDB 纯粹作为后端解决方案,而您的页面由另一台服务器提供服务也是一个很好的选择,但缺点是跨域问题使您无法轻松使用内置的 CouchDB API。

那么有人能解决这两个缺点之一吗?

您能否以某种方式从 CouchApp 提供服务器端代码(甚至 PHP 也将是一个胜利),或者您能否以某种方式使用单独的站点来为您的页面提供服务但克服跨域问题?

我真的试图让解决方案尽可能干净(和可扩展),CouchDB 的伟大之处之一就是超级简单、超级快速的 API,所以我真的不想在它周围使用包装器 - 除非它不这样做不妨碍性能/可扩展性。

欢迎您提出意见。

I have been playing with CouchDB and CouchApp for a little while now. I am planning on using it for a new web site project I'm working on.

  1. From a scalability viewpoint I love the idea of CouchApp.

    The downside is that with no server side code there are some things (like oAuth authentication) that are hard to do on the client side. At some point I'm sure I will need some server side code of some description - I guess you can then look at Node.js but would rather not at this point.

  2. Using CouchDB purely as a backend solution while your pages are served from another server is also a great fit but the downside here is that the cross domain issues prevent you from easily using the built in CouchDB API.

So does anyone have a solution to one of these two downsides?

Can you somehow serve server side code (even PHP would be a win) from CouchApp or can you somehow use a separate site to serve your pages but overcome the cross domain issue?

I am really trying to keep the solution as clean (and as scalable) as possible and one of the great things about CouchDB is the super simple, super fast API so I don't really want to use a wrapper around it - unless it doesn't hinder the performance/scalability.

Your opinions are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-10-08 18:39:16

由于 CouchDB 使用基于 HTTP 的 API,因此有多种方法可以将服务器端代码(node.js、PHP 等)与 CouchApp 进行“混合和匹配”。

我将这些选项分为三类:

  1. 首先,您现在拥有的是 2 层架构:浏览器 + 由 CouchDB 提供服务的 CouchApp。对于只需要浏览器和 CouchDB 所能提供的功能的应用程序来说,这是一个很好的解决方案,但是当您需要发送电子邮件、调整图像大小或从另一个不需要的数据库获取数据时,您将需要另一层有一个 HTTP API(MySQL、MongoDB 等)。
  2. 接下来是 3 层架构:浏览器 + Apache/PHP(或类似堆栈)+ CouchDB。这是更“传统”的选项(即 LAMP)。这对于逐步迁移到 CouchDB 来说很好,但从长远来看,必须通过第二个 HTTP 服务器(也许作为代理)或服务器端脚本语言(如 PHP)路由所有内容可能会变得很麻烦。
  3. 最后,也是我最喜欢的,是 2.5 层架构:浏览器 + CouchDB + 外部或 _changes 基于提要的“操作”。在这种情况下,PHP(或类似的)充当 CouchDB 的某种服务提供者。可以通过让 PHP 监视特定类型文档及其更改(即图像上传、电子邮件消息文档)的 _changes feed 来触发操作,或者可以将 CouchDB 设置为“ping”_external 处理程序以对文档进行进一步处理或其附件。这本质上就是 couchdb-lucene 的工作原理,即监视更新并在更新发生时或定期对其采取行动。

您可以在下面找到有关 _changes feed 和 _external 处理程序的更多信息:

就其价值而言,我将在下周三的 PHP 和 CouchDB 网络广播。您的问题将为网络广播结束时的讨论提供宝贵的补充。

我很想知道您的 CouchApp 结果如何,以及您如何解决上述问题。

Because CouchDB uses an HTTP-based API there are several ways to "mix and match" server-side code (node.js, PHP, etc) with your CouchApp.

I break the options down into three categories:

  1. First, a 2 Tier Architecture is what you have now: Browser + CouchApp served from CouchDB. It's a great solution for apps that only need what the browser and CouchDB can offer, but you'll need another tier as soon as you hit the need to send e-mail, resize images, or get data from another database that doesn't have an HTTP API (MySQL, MongoDB, etc).
  2. Next, is the 3 Tier Architecture: Browser + Apache/PHP (or similar stack) + CouchDB. This is the more "traditional" option (i.e., LAMP). This is fine for gradually migrating to CouchDB, but long term, it can get cumbersome having to route everything through a second HTTP server (as a proxy, perhaps) or server-side scripting language like PHP.
  3. Last, and my favorite, is the 2.5 Tier Architecture: Browser + CouchDB + externals or _changes feed-based "actions." In this case, PHP (or similar) acts as a service provider of sorts to CouchDB. Actions can be triggered by either having PHP watch the _changes feed for certain types of documents and their changes (i.e., image upload, email message document), or CouchDB can be setup to "ping" an _external handler to do further processing on the document or its attachments. This is essentially how couchdb-lucene works by watching for updates and taking action on them as they happen or at regular intervals.

You can find out more about the _changes feed and _external handlers below:

For what its worth, I'll be discussing these three options this coming Wednesday in a PHP and CouchDB Webcast. Your questions would be a valuable addition to the discussion at the end of the webcast.

I'd love to know how your CouchApp turns out, and how you solve the problems you mentioned above.

人生百味 2024-10-08 18:39:16

BigBlueHat 的回答非常好,但我还有一个可能的选择要添加:

克服跨域问题,从而允许您从任何类型的 Web 服务器提供页面,并允许浏览器直接与 CouchDB 交互(这可以是在另一台服务器上)。

由浏览器强制执行的跨域限制并不难绕过。
我知道的两种主要方法是:JSONP 和 CORS。

JSONP 将每个请求伪装为“脚本”请求(因为脚本请求不受跨域规则的约束)。它仅适用于 GET,不适用于 POST 或 PUT 或其他任何内容。您可以使用 jQuery 来完成。

CORS 是“跨源资源共享”,它只是一个必须在服务器(本例中为 couchdb)上实现的特殊 HTTP 标头,它告诉浏览器它没问题——它不介意服务来自以下位置的请求:另一个域。我已经对此进行了测试,它确实有效,但可能存在安全问题 - 我不确定。

所以...我不知道这是否是一个好主意,但技术上可以(至少部分地)克服 CouchDB 的跨域限制。有人曾经使用这种类型的设置构建过系统吗?

Excellent answer from BigBlueHat, but I have one more more possible option to add:

Overcome the cross-domain problem, thereby allowing you to serve pages from any sort of web server, and also allowing the browser to interact directly with CouchDB ( which can be on another server ).

The cross-domain restriction, as enforced by browsers, is not that hard to get around.
The 2 main methods that I know of are: JSONP and CORS.

JSONP disguises each request as "script" request ( as script requests are exempt from the cross-domain rule). It only works for GET, not POST or PUT or anything else. You can do it with jQuery.

CORS is "Cross Origin Resource Sharing", and it is simply a special HTTP Header that must be implemented on the server ( couchdb in this case ), which tells the browser that it's OK -- it doesn't mind serving requests that come from another domain. I have tested this, and it does work, but there might be security issues -- I am not sure.

So...I don't know whether it's a good idea, but it is technically possible to ( at least partially ) overcome the cross-domain restriction with CouchDB. Has anyone ever built a system using this type of setup?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文