TerriaJS Server

4.0.4 · active · verified Sun Apr 19

TerriaJS-Server is a foundational Node.js Express server designed to complement the TerriaJS geospatial platform. It provides essential backend services for web-based 2D and 3D geospatial data explorers, including a robust CORS proxy for accessing data providers that lack proper CORS headers, a `proj4` Coordinate Reference System (CRS) lookup service, an `ogr2ogr` conversion service for unsupported geospatial vector data formats (like shapefiles) to GeoJSON, and services for persistent sharing of map configurations. Currently at stable version 4.0.4, published recently, the package undergoes active maintenance with updates driven by the needs of the broader TerriaJS ecosystem. Its key differentiators lie in its specialized geospatial services and deep integration with TerriaJS and TerriaMap, enabling rich, interactive mapping applications, particularly within the context of National Map projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic initialization and startup of the TerriaJS Server with a basic configuration, including static file serving and environment variable usage for secrets. This is useful for embedding or custom deployments.

import express from 'express';
import path from 'path';
import { createTerriaServer } from 'terriajs-server';

// Minimal server configuration - typically read from serverconfig.json
const serverConfig = {
  port: process.env.PORT ?? 3001,
  public: true,
  // Define allowed domains for the proxy, crucial for security
  allowProxyFor: [
    'https://example.com',
    'https://another-domain.org'
  ],
  // Optional: Share service configuration (e.g., Gist, S3)
  share: {
    type: 'gist',
    accessToken: process.env.GITHUB_GIST_TOKEN ?? '' // Required for Gist
  },
  // Other TerriaJS-Server specific configurations go here
};

async function startCustomTerriaServer() {
  try {
    // createTerriaServer returns an Express application instance
    const app = await createTerriaServer(serverConfig);

    // Serve static files from a 'wwwroot' directory (e.g., where TerriaMap is built)
    const wwwrootPath = process.env.WWWROOT_PATH ?? path.join(process.cwd(), 'wwwroot');
    app.use(express.static(wwwrootPath));

    app.listen(serverConfig.port, () => {
      console.log(`TerriaJS Server listening on port ${serverConfig.port}`);
      console.log(`Serving static files from: ${wwwrootPath}`);
    });
  } catch (error) {
    console.error('Failed to start TerriaJS Server:', error);
    process.exit(1);
  }
}

startCustomTerriaServer();

view raw JSON →