Sample Application
Discover how to program interactions with the Couchbase Server via the data, Query, and search services — using the Travel Sample Application with the built-in Travel Sample data Bucket. This version demonstrates various features of Couchbase, including the Collections feature new in Server 7.0.
Quick Start
Fetch the Couchbase Node.js SDK travel-sample Application REST Backend from github:
git clone https://github.com/couchbaselabs/try-cb-nodejs.git
cd try-cb-nodejs
With Docker installed, you should now be able to run a bare-bones copy of Couchbase Server, load the travel-sample, add indexes, install the sample-application and its frontend, all by running a single command:
docker-compose up
Running the code against your own development Couchbase server.
For Couchbase Server 7.0, make sure that you have at least one node each of data; query; index; and search. For a development box, mixing more than one of these on a single node (given enough memory resources) is perfectly acceptable.
If you have yet to install Couchbase Server in your development environment start here.
Then load up the Travel Sample Bucket, using either the Web interface or the command line. You will also need to create a Search Index — Query indexes are taken care of by the Sample Bucket.
See the README at https://github.com/couchbaselabs/try-cb-nodejs for full details of how to run and tweak the Node.js SDK travel-sample app.
Using the Sample App
Give yourself a username and password and click Register.
Now try out a few queries, and see Search in action for the hotel finder feature.
Sample App Backend
The backend code shows Couchbase Node.js SDK in action with Query and Search, but also how to plug together all of the elements and build an application with Couchbase Server and the Node.js SDK.
Here’s the airport search code, which checks to see whether the search term for the query string is a three or four letter FAA or ICAO abbreviation, and if not searches for it as an airport name:
var qs;
if (searchTerm.length === 3) {
// FAA code
qs = `SELECT airportname from \`travel-sample\` WHERE faa = '${searchTerm.toUpperCase()}';`;
} else if (searchTerm.length === 4 &&
(searchTerm.toUpperCase() === searchTerm ||
searchTerm.toLowerCase() === searchTerm)) {
// ICAO code
qs = `SELECT airportname from \`travel-sample\` WHERE icao = '${searchTerm.toUpperCase()}';`;
} else {
// Airport name
qs = `SELECT airportname from \`travel-sample\` WHERE LOWER(airportname) LIKE '%${searchTerm.toLowerCase()}%';`;
}
let result, rows;
try {
result = await cluster.query(qs);
rows = result.rows;
} catch (error) {
console.error(error)
};
res.send({
data: rows,
context: [qs]
});
The index.js file also contains the functions for handling users, registration, and N1QL queries.
Data Model
See the Travel App Data Model reference page for more information about the sample data set used.