0
votes

I'm using Marklogic npm module v2.1.1 with my express(v4.16.4) Node app. In my controller function I'm using the db.eval method to return the data along with necessary status code using function xdmp.setResponseCode(). Please find the below code snippet

// controller function 
function getNewsArticleById(db: any, fileName: string, req: any, res: any, next: any) {
  return db.eval(
     `xdmp.setResponseCode(403, 'Forbidden');
     {data: 'abc'};`,
     {},
     })
    .result(
      data => {
        res.json(data[0].value);
      },
      err => {
        console.log(err);
        next(err);
      });
}

The above function is attached to a route t return the desired response which could be either a success (200 response codes) or failures (400 response codes). The success case works fine but when ever the code sets to 400 or greater response code I get a 500 error saying

eval JavaScript on server: cannot process response with 403 status.

To me, it seems to be a limitation of the db.eval() MarkLogic function

Can you please suggest me how to fix this? If not, what should be best alternative to handle it?

1
you mean you directly pass this function to express route? you can not have db and filename as first arguments. express doesnt work like this with the function that you provided for itHassan Gilak
No, this is just my controller function which which take other arguments along with req and res. my route is as below: const db = {<connection>}; const filename = 'fileName'; app.get ('/data', (req, res, next) => { getNewsArticleById(db, fileName, req, res); })Rachit Rampal

1 Answers

2
votes

The typical approach for evaluated code would be to return an error value instead of setting an HTTP status code.

If you want to provide an HTTP interface to your scripting code on the MarkLogic enode, you might consider writing and installing an endpoint. An endpoint has complete control over the HTTP response.

You might also write and install a resource service extension, which is invoked by the REST API but can specify an error HTTP status when throwing an error as described here:

http://docs.marklogic.com/guide/rest-dev/extensions#id_20992

Likely stating the familiar, installing an endpoint or resource service extension is also more secure than sending code for evaluation from a client with a privileged role.

Hoping that helps,