Fetching contributors…
Cannot retrieve contributors at this time
134 lines (118 sloc) 3.29 KB


The BaseX Node client is asynchronous. Most functions take an optional callback function as last argument. The callback function will be called with two arguments when the results are available . The first err will contain any error information or null if there is no error The second reply will hold the reply. The reply is often an object with result and info properties.

	{ result: '1 2 3 4 5 6 7 8 9 10',
	  info: '\nQuery executed in 0.38 ms.\n' }

The utility function print in the debug module shows the syntax:

	function print(err, reply) {
		if (err) {
			console.log("Error: " + err);
		} else {

The API is modelled closely on the BaseX client protocol See the examples for more details

Create a Client Session

        var basex=require("basex");
		var session=basex.Session(host, port, username, password)

The default values are: hostname (default="localhost") port (default=1984) username (default="admin") password (default= "admin")

Since version 0.8.1 the session object emits socketError events on socket errors. This allows an application to react to events such as the server doing down.(See examples/issue22.js)

Session commands



Executes a command on the server calls callback with the result. Example

client.execute("create db test_db", log.print);


	var query=session.query(query)

Returns a query object for the specified query, see below for more detail. Example

var input = 'for $i in 1 to 100 return <xml>Text { $i }</xml>';
var query = session.query(input);



Creates a database from an input stream.

// create new database
client.create("test_db", "<x>Hello World!</x>", log.print);



Adds a document to the current database from an input stream or a string. Example

// add document from stream
var s=fs.createReadStream(__dirname+ "/books.xml");
client.add("/world/World.xml", s, log.print);



Replaces a document with the specified input stream.


Stores raw data at the specified path.

##info Returns process information.

##close session.close(callback) Closes the session.

The query object

Create a query object var q=session.query(query), then bind any external variables, finally call results orexecute` ##bind


Binds a name to a value. Currently type is ignored.

query.bind("name", "nodex","",log.print); 

query.close(); ##results
query.results(callback); Returns results as an array.




Executes the query and returns all results as a single string.




The index.js module variable debug_mode can be set to true to print diagnostic info to the console.