如何在Marklogic Grove中的node.js中处理REST API? [英] How do I handle the REST API in node.js in Marklogic Grove?

查看:53
本文介绍了如何在Marklogic Grove中的node.js中处理REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MarkLogic Grove(React)开发UI应用程序.

I am developing a UI application using MarkLogic Grove(React).

默认情况下,来自Web浏览器的REST API调用将直接转发到MarkLogic.

By default, REST API calls from web browser will be forwarded directly to MarkLogic.

例如:http://主机名/v1/resources/foo/

For example: http://hostname/v1/resources/foo/

如果REST API是特定路径,我想按原样在node.js中进行处理,而无需将调用转发给MarkLogic.我应该在哪里以及如何实施它?(仅对经过身份验证的用户可用).

If the REST API is a specific path, I would like to do the processing in node.js as it is without forwarding the call to MarkLogic. Where and how should I implement it?(Only available to authenticated users.)

例如:http://主机名/my-rest-api/bar

For example:http://hostname/my-rest-api/bar

我想要一些建议,因为Grove的源代码太多,而且令人困惑.

I'd like some advice as there is too much source code for Grove and it's confusing.

推荐答案

Grove的文档还不存在.如果要讨论对MarkLogic后端的封送处理前端调用,则是在谈论Grove中间层: grove-节点.该子项目中嵌入了一些文档,这可能是最好的起点.

The documentation of Grove is not quite there yet, unfortunately. If talking about marshaling frontend calls to the MarkLogic backend, you are talking about the Grove middle-tier: grove-node. There is some documentation embedded in that subproject, which is probably the best place to start.

该子项目的顶级自述文件有一个指向进一步内容的指针关于端点和路由的文档.它也没有提供很多帮助,但是它确实告诉您在哪里寻找.树林节点中间层基本上是 ExpressJS 服务器.我们在一个子模块中隐藏了主要的,主要是静态的逻辑,您可以在其中找到

The top-level README of that subproject has a pointer to further documentation about Endpoints and Routes. It doesn't provide a lot of help either, but it does tell you where to look. The grove-node middle-tier is basically an ExpressJS server. We tucked away the main, and mostly static logic in a submodule that you can find here. The important part, the actual business logic (called middle-ware) has been put inside the folder called routes/.

尽管我们试图以默认Route的形式提供许多常用功能,但您仍可以在其中编写/添加任何您喜欢的ExpressJS逻辑.您可以在 routes/api/index.js 顶部附近找到 routeFactory .

In there you can write/add any ExpressJS logic you like, although we tried to provide a number of commonly used functionalities in the shape of default Routes. You can find the routeFactory near the top of routes/api/index.js.

master分支(包含grove-node的最新版本)是最新的,但是克隆该存储库的development分支并替换/更新所生成的 middle的内容仍然有用.项目中的-tier/文件夹,其中包含该开发分支的内容.您应该能够做一个相当简单的目录比较,以应用看起来有价值的更新.

The master branch, containing the last release of grove-node, is fairly up to date, but it could still be useful to clone the development branch of that repository, and replace/update the contents of the generated middle-tier/ folder in your project with the contents from that development branch. You should be able to do a fairly straight-forward directory compare to apply updates that look worthwhile.

develop分支至少包含一个名为Grove defaultRestRoute的新添加项,它允许将随机URI重写为MarkLogic中的任何新目标URI.本来是用于REST扩展的,但是对于映射到数据服务,/v1/values,/v1/rows或/v1/sparql调用也非常有用.这比传统的白名单代理要好得多,后者通常是为了快速解决和向后兼容而维护的.

The development branch contains at the least a new addition called the Grove defaultRestRoute, which allows rewriting random URIs to any new target URI in MarkLogic. Originally meant for REST extensions, but very useful to map to Data Services, /v1/values, /v1/rows, or /v1/sparql calls as well for instance. It is a much better approach than the legacy whitelist proxy, which we mostly maintained for quick workarounds, and backwards-compatibility.

下面是使用defaultRestRoute的示例,该代码被附加在 middle-tier/routes/api/index.js 的末尾附近:

Here an example of using the defaultRestRoute, this code was appended near the end of middle-tier/routes/api/index.js:

// Special case for Raw Media
router.use('/crud/Media/:mediaId/:binaryType', function(req, res, next) {
  let mediaId = decodeURIComponent(req.params.mediaId);
  let binaryType = decodeURIComponent(req.params.binaryType);
  return routeFactory.defaultRestRoute({
    authProvider: authProvider,
    authed: true, // default: true
    neverCache: true, // default: true
    action: {
      uri: '/v1/documents',
      GET: function() {
        return {
          method: 'GET',
          body: null,
          params: {
            uri: '/Media/' + mediaId + '/binary.' + binaryType
          }
        };
      }
    }
  })(req, res, next);
});

它在具有CRUD终结点的应用程序中使用,该终结点用于名为Media的实体.上面的路由提供对媒体文件实际二进制文件的访问,而普通的CRUD GET调用返回包含元信息的Entity信封.

It is used in an application that has a CRUD endpoint for an Entity called Media. The above route provides access to the actual binary of the media file, while the ordinary CRUD GET call returns the Entity envelope containing meta information.

这篇关于如何在Marklogic Grove中的node.js中处理REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆