Skip to main content

Node.js Adapter

Overview

The Node.js adapter is designed for the Node.js environment, providing access to CloudBase services. It enables developers to utilize CloudBase features such as databases, storage, and functions in Node.js applications.

Installation

Install the Node.js adapter using npm:

npm i @cloudbase/adapter-node

Quick Start

Usage

const cloudbase = require("@cloudbase/js-sdk");
const adapter = require("@cloudbase/adapter-node");

// Use the Node.js adapter
cloudbase.useAdapters(adapter);

const app = cloudbase.init({
env: "your-env", // Replace with your actual environment id
});

Usage Example

Cloud Function Invocation Example

// Cloud Function Invocation Example
const result = await app.callFunction({
name: "your-function-name",
data: {
key: "value"
}
});

Database Operations Example

// Database Operations Example
const db = app.database();
// Create a test collection
const testCollection = db.collection('test_collection');
// Insert test data
const insertResult = await testCollection.add({
name: "Node.js Test",
timestamp: new Date().toISOString(),
});

File Upload Example

// File Upload Example
const fileName = `test-files/app-upload-${Date.now()}.jpg`;

const fs = require('fs');
const testFilePath = '/your/local/path/XXX.jpg'; // Replace with your local file path
// Read file content
const fileBuffer = fs.readFileSync(testFilePath);

const uploadResult = await app.uploadFile({
cloudPath: fileName,
filePath: fileBuffer,
onUploadProgress: function (progressEvent) {
console.log("Upload progress:", progressEvent);
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
});