Cube.js Server

0.33.8 · active · verified Sun Apr 19

Cube.js Server (`@cubejs-backend/server`) is the core Node.js component of the Cube.js analytics platform, an open-source semantic layer for building analytical applications. It functions as a backend microservice that manages connections to various data sources (SQL databases, data warehouses), handles query queuing, caching, and pre-aggregations, and exposes a GraphQL/REST API for frontend applications. The current stable version, as per recent npm releases, is around 1.6.x, while the user provided 0.33.8 which is an older minor version. Cube.js releases new versions frequently, sometimes introducing breaking changes even in minor updates within a major series (e.g., 0.x.x versions). Its key differentiators include a SQL-based data schema for defining measures and dimensions, advanced pre-aggregation for performance, robust caching, enterprise-grade security (JWT tokens, row-level security), and visualization-agnostic API support, allowing integration with any frontend framework or BI tool.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and launch a basic Cube.js API server using Express. It shows essential configuration for database connection, API secret, port, and enabling the developer playground.

import express from 'express';
import { CubejsServer } from '@cubejs-backend/server';
import dotenv from 'dotenv';
import path from 'path';

dotenv.config({ path: path.resolve(process.cwd(), '.env') });

async function main() {
  const server = new CubejsServer({
    // Required for security and API token generation
    // Use process.env.CUBEJS_API_SECRET ?? '' for production
    apiSecret: process.env.CUBEJS_API_SECRET ?? 'YOUR_API_SECRET',
    
    // Configure your database connection
    dbType: process.env.CUBEJS_DB_TYPE ?? 'postgres',
    dbHost: process.env.CUBEJS_DB_HOST ?? 'localhost',
    dbName: process.env.CUBEJS_DB_NAME ?? 'cubejs_data',
    dbUser: process.env.CUBEJS_DB_USER ?? 'cubejs_user',
    dbPass: process.env.CUBEJS_DB_PASS ?? 'cubejs_password',
    
    // Path to your Cube.js schema files (e.g., .js, .yml)
    // Defaults to 'schema' folder in the root
    // schemaPath: path.resolve(process.cwd(), 'schema'),

    // Port for the Cube.js API
    port: parseInt(process.env.PORT ?? '4000', 10),

    // Enable developer playground in development mode
    devPlayground: process.env.NODE_ENV === 'development',

    // Additional server configuration options
    // full stack traces for development debugging
    extendContext: (req) => ({
      traceId: req.headers['x-request-id'] || 'no-trace',
    }),
  });

  const app = express();
  // Integrate Cube.js with your Express app
  await server.initApp(app);

  app.listen(server.options.port, () => {
    console.log(`🚀 Cube.js server is running on http://localhost:${server.options.port}`);
    if (server.options.devPlayground) {
      console.log(`▶️ Cube.js Playground available at http://localhost:${server.options.port}`);
    }
  });
}

main().catch((e) => {
  console.error('Failed to start Cube.js server:', e);
  process.exit(1);
});

view raw JSON →