How do I call the backend MarkLogic API from my own API in MarkLogic Grove?

戏子无情 提交于 2020-12-15 01:42:04

问题


This question is a continuation of the question below.

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

I was able to implement a sample REST API that can be called when authenticated with Grove, as shown below.

middle-tier/routes/index.js

const authProvider = require('../../grove-node-server-utils/auth-helper');

router.get('/my-rest-api/bar',(req, res) => {
  const response = {status : "success"};
  authProvider.isAuthenticated(req,res,()=>{
    res.send(response);
  });
});

Next, I want to call the MarkLogic API from within the my-rest-api. I wrote the following through trial and error, but an error occurs.How should I write it?

middle-tier/routes/index.js

const authProvider = require('../../grove-node-server-utils/auth-helper');
const backend = require('../../grove-node-server-utils/backend');

router.get('/my-rest-api/bar',(req, res) => {
  const myResponse = {status : "success!!"};
  console.log("start");
  const backendOptions = {
    method: 'GET',
    path: '/v1/resources/myMarkLogicAPI?p1=test'
  };

  authProvider.isAuthenticated(req,res,()=>{
    console.log("preprocessing");
    backend.call(req, backendOptions, () => {
      console.log("postprocessing")
      res.send(myResponse);
    });
  });
});

The error message is: {"message":"TypeError: Cannot convert undefined or null to object"}


回答1:


I suspect the code is expecting a headers property as part of backendOptions. We typically wrap the backend call in a authProvider.getAuth call that returns an authentication header that we pass through to the backend call. You may want to do the same to be sure you are allowed to access the backend.

That said, it may be simpler to use our new DefaultRestRoute as mentioned in your previous question. I showed some sample code in there as well.

HTH!



来源:https://stackoverflow.com/questions/64870662/how-do-i-call-the-backend-marklogic-api-from-my-own-api-in-marklogic-grove

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