使用 python-eve 调用 javascript 过程

calling javascript procedure with python-eve

有没有办法在我的 GET 请求中实现一些 logic/coding。

例如在 mongodb 上调用 javascript 过程。

背景是我想计算给定地理多边形内存储的地理点的平均值。

此致 哈拉尔德

您可以将回调函数挂接到所有 database and requests 事件。

>>> def add_average(resource, response):
...     response['average values'] = my_average_values

>>> app = Eve()
>>> app.on_fetched_item += add_signature

从您的回调中,您可以执行 PyMongo eval 来执行 JavaScript 代码。但是请记住,eval 自 Mongo 3.0 以来已被弃用,因此不推荐使用。

引用 MongoDB 的 Asya Kamsky:

In general, performance of eval will be poor and I would recommend implementing in Python anything you were planning on implementing in JS if you are writing a Python application.