Node.js 可以像 CouchApp 一样执行从 CouchDB 中提取的 JavaScript 函数吗?如何?
快速概述是这样的:对于我的 Web 应用程序,我可以使用 CouchApp 和 CouchDB 视图等编写大部分功能。我喜欢 CouchApp 的功能,它通过复制将我的代码推送到服务器 - 这使得部署周期非常简单。
然而,为了完成一些在 couchdb 中不支持的任意工作并解决一些限制,我需要在 CouchDB 前面放置一个 Web 平台。我正在考虑在 node.js 中构建它,因为它使用 JavaScript,并且我想继续将代码推送到数据库的简单部署方法。
我想象它是这样工作的: - 我使用正常方法和节点命令在node.js中编写一个Web服务器/服务来启动它。 - 该服务连接到 couch 数据库并获取虚拟列表和 URL 映射列表。该列表存储在redis中,以便快速查找。该列表将告诉服务器,当它收到请求时,根据主机和路径等,要运行哪个处理程序。 - 服务器获取处理程序 - 这只是一个文档,它可以是一个设计文档或 couchdb 中的任意 json 文档。然后执行该处理程序来处理请求,就像我将处理程序编写为节点 js 的一部分一样。
那么问题是,如何以文本形式获取其中包含 JavaScript 函数的子数据结构,并执行该函数?
这可能是非常明显的,但我来自编译背景,所以通常这里会有一个编译步骤,这使得这几乎是不可能的。
所以,我的想法是伪代码: var string thecode = getValueForMapKey(handlerFunctionIWant); 是否
有一个 exec 或 run 函数可以在 JavaScript 中执行上述神奇的执行步骤?
The quick overview is this: for my web app I can write most of my functionality using CouchApp and CouchDB views, etc. I love the feature of CouchApp that pushes my code up to the server via replication- this makes the deployment cycle very easy.
However, to do some arbitrary work not supported in couchdb and works around a few limitations, I need to put a web platform in front of CouchDB. I'm considering building this in node.js because it uses JavaScript and I want to continue the easy deployment method of pushing code into the database.
Here's how i imagine it working:
- I write a web server/service in node.js using the normal method and the node command to start it.
- this sevice connects to couch db and gets a virtual list and a URL mapping list. This list is stored in redis for quick lookup. This list will tell the server, when it gets a request, based on host and path, etc, which handler is to be run.
- the server fetches the handler- which is just a document, it could be a design document or an arbitrary json document in couchdb. And then executes that handler to handle the request, as if I'd writte the handler as part of node js.
So the question is, how to get a son data structure that contains a JavaScript function in it, in text form, and execute that function?
This may be blindingly obvious, but i come from a compiled background, so normally there would be a compilation step here that makes this pretty much impossible.
So, what I'm thinking is in pseudo code:
Var string thecode = getValueForMapKey(handlerFunctionIWant);
somehowmagicallyexecute(thecode)
Is there an exec or run function that will do the magical execution step above in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它将在 node.js 上下文中运行。
您还可以在节点中使用它,就像这样,作为动态函数:
使您的文档看起来像这样:
它与调用新函数的范围相同,因此您可以访问摇篮,或者您可以需要其他库,它将被加载,就好像它是该范围内的匿名函数一样。
将其放入设计文档中,然后只有管理员可以立即进行更改。
这是一个更好但类似的方法:
这将输出:
It will run in the node.js context.
You can also use it in node, like this, as a dynamic function:
make your docs look like this:
It's in the same scope as where the new Function is called, so you have access to cradle, or you can require other libs, which will be loaded as if it was an anon function in that scope.
Put it in a design doc, then only admin can make changes, out of the box.
Here is a nicer, but similar approach:
this will output:
eval(handlerFunctionIwant) 是执行它的调用。当然,您需要确保黑客无法将代码注入该字符串。
我不清楚这是否会在可以访问其他 javescript 资源(例如 node.js 的其余部分或访问您的 couchdb 库)的上下文中对其进行评估。
eval(handlerFunctionIwant) is the call to execute it. You need to make sure that there's no way for hackers to inject code into that string, of course.
It is not clear to me if this will evaluate it in a context that has access to other javescript resources, such as the rest of node.js or access to your couchdb library.