A BaseX REST Client for Node.js
npm install node-basex
BaseX REST Client provides an easy way to query a BaseX (XML Database) HTTP
server through its REST API using the
POST method.
$ npm install node-basex
`$3
`javascript
var basex = require('node-basex');var client = new basex.Client({});
var query = {
text: 'for $i in //example return $i'
};
client.query(query, function (data) {
console.log(data); // Response data
});
`In Depth
The BaseX REST API documentation
contains information on how a POST query should be structured for communicating
with Basex and will help clarify the various options specified below.$3
The BaseX REST Client is created by providing an object containing all the
options for the connection. The options are option and include host, port,
path, username, and password. Any option omitted will use the default
BaseX value.`javascript
var basex = require('node-basex');
var client = new basex.Client({
host: 'localhost',
port: 8984,
path: '/rest/mydatabase',
username: 'admin',
password: 'admin'
});
`*Note: An empty object is required for the options even if you are using
defaults*
$3
Queries follow the BaseX REST POST Schema
and can consist of text, context, parameter, variable and option elements.#### Text
The text element is used to specify the query operation.
`javascript
var query = {
text: 'for $i in //example return $i'
};
`#### Context
The context parameter is used to provide an initial XML context node.
`javascript
var query = {
context: 'context'
};
`#### Options
Options are applied before the actual
query operation (text) is performed.
`javascript
var query = {
options: [
{key: 'option1', value: 1},
{key: 'option2', value: 2}
]
};
`#### Variables
Variables allow for
external variables to be set that can be referenced from the text element.
`javascript
var query = {
variables: [
{key: 'var1', value: 1},
{key: 'var2', value: 2}
]
};
`#### Running the Query
When the query is run, any response data will be returned via the callback so
long as the BaseX HTTP server is running.
`javascript
client.query(query, function (data) {
// Response data can be null
});
`$3
`
$ npm install -g gulp mocha
$ npm test
``