How do I handle the REST API in node.js in Marklogic Grove?

我们两清 提交于 2021-01-05 12:44:32

问题


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

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

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

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.)

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

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


回答1:


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.

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/.

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.

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.

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.

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);
});

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.



来源:https://stackoverflow.com/questions/64747392/how-do-i-handle-the-rest-api-in-node-js-in-marklogic-grove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!