Skip to content

Latest commit

 

History

History
97 lines (68 loc) · 3.06 KB

sample-application.adoc

File metadata and controls

97 lines (68 loc) · 3.06 KB

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 also demonstrated the Developer Preview of the upcoming Collections feature.

6.5@sdk:shared:partial$sample-application.adoc

Preparation

As well as the Node.js SDK 3.0 and Couchbase Server, set up as described above, you will need git to fetch the travel sample application code:

git clone https://github.com/couchbaselabs/try-cb-nodejs.git

Change directory into your cloned repository, and check out the latest branch (this will most probably be enabled as the default branch).

cd try-cb-nodejs
git checkout 6.5
npm install

Running the Travel Sample Application

Next, edit the index.js file to point to one for your containerised Couchbase Server (or localhost, 127.0.0.1, if appropriate), and any other configuration changes — such as password. From here onwards, we’ll assume the defaults.

And run with

npm run start

After the build, with your Web browser of choice, head to port 8080 of the local machine — http://localhost:8080.

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.