You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何配置Couchbase Server与Sync Gateway?附应用连接代码咨询

Setting Up Couchbase Server & Sync Gateway Connection

Hey there! Since you’ve already got your Express app connected to Couchbase Server successfully, let’s walk through how to link that server up with Couchbase Sync Gateway step by step.

1. Create a Sync Gateway Configuration File

First, you’ll need a JSON config file that tells Sync Gateway how to connect to your Couchbase Server cluster, which bucket to use, and how to handle sync rules. Here’s a config tailored to your existing setup (using your non-med bucket and admin credentials):

{
  "log": ["*"],
  "databases": {
    "non-med": {
      "server": "couchbase://localhost",
      "username": "Administrator",
      "password": "ABcd1234",
      "bucket": "non-med",
      "users": {
        "GUEST": { "disabled": false, "admin_channels": ["*"] }
      },
      "sync": `function(doc, oldDoc) {
        // Simple sync rule: assign docs to their specified channels, or default
        channel(doc.channels || ["default"]);
      }`
    }
  },
  "interface": ":4984", // Public REST API port for client apps
  "adminInterface": ":4985" // Admin port for configuration/monitoring
}

Let’s break down the key bits:

  • server: Matches the cluster address you used in your Express app (couchbase://localhost)
  • username/password: Uses your existing Couchbase Server admin credentials
  • bucket: Targets your non-med bucket directly
  • users: Enables a guest user for testing (you’ll want to lock this down for production)
  • sync: A basic sync function that routes documents to channels (customize this for your app’s needs)

2. Start Sync Gateway

Once you’ve saved the config as sync-gateway-config.json, launch Sync Gateway using the command line:

On macOS/Linux:

./sync-gateway sync-gateway-config.json

On Windows:

sync_gateway.exe sync-gateway-config.json

When it starts, you’ll see logs confirming it’s connected to your Couchbase Server and non-med bucket.

3. Verify the Connection

To make sure everything’s linked up correctly, test the Sync Gateway API:

  • Open your browser or use curl to hit http://localhost:4984/non-med
  • You should get a JSON response like this (showing bucket details):
    {
      "db_name": "non-med",
      "doc_count": 0,
      "update_seq": 0,
      "committed_update_seq": 0,
      "disk_size": 0,
      "purge_seq": 0,
      "other": {
        "data_size": 0
      },
      "instance_start_time": "1699999999999",
      "compact_running": false
    }
    

4. Optional: Update Your Express App to Use Sync Gateway

If you want your app to interact with Couchbase via Sync Gateway (to leverage sync features for mobile clients, for example), you can adjust your code to use Sync Gateway’s REST API instead of the direct Couchbase SDK. Here’s a quick example using your existing request library:

// Create a document via Sync Gateway
request.post({
  url: 'http://localhost:4984/non-med/test-document',
  json: {
    title: 'Sync Gateway Test',
    content: 'This doc was added via Sync Gateway',
    channels: ["default"]
  }
}, (err, resp, body) => {
  if (!err && resp.statusCode === 201) {
    console.log('Document created successfully:', body);
  }
});

Quick Notes for Production:

  • Disable the GUEST user and set up proper authenticated users
  • Refine the sync function to match your app’s channel and access control needs
  • Ensure Sync Gateway and Couchbase Server versions are compatible (check official docs for matching versions)

内容的提问来源于stack exchange,提问作者mandy Yiu

火山引擎 最新活动