Basic

Basic Server — Device Routes

Example wiring of `DeviceStore`, `CertManager`, and Hub device routes.

server.js — Wire up hub-server routes

const express = require('express');
const { createDeviceRegistry, createLogger } = require('@epheme/core');
const DeviceStore = require('../../packages/hub-server/deviceStore');
const CertManager = require('../../packages/hub-server/certManager');
const { registerDeviceRoutes } = require('../../packages/hub-server/deviceRoutes');
const { eventBus, emitHubEvent } = require('../../packages/hub-server/eventBus');

const app = express();
app.use(express.json());

const logger = createLogger({ service: 'hub', component: 'device-routes' });
const deviceStore = new DeviceStore();
const certManager = new CertManager();

const { issueDeviceJWT, verifyDeviceJWT } = createDeviceRegistry({ 
  deviceJwtSecret: process.env.DEVICE_JWT_SECRET || 'dev-secret',
  deviceJwtTtl: 3600 
});

registerDeviceRoutes(app, () => deviceStore, { 
  certManager, 
  issueDeviceJWT, 
  verifyDeviceJWT, 
  deviceJwtTtl: 3600,
  emitHubEvent,
  tenantSlug: process.env.TENANT_SLUG || 'default',
  patStore: null,
  isDeviceAutoApprove: false,
  pubClient: null,
  rateLimiters: {}
});

app.listen(3000, () => logger.info('Hub device routes listening on 3000'));

Key parameters:

  • certManager — X.509 device cert manager (optional if using token auth only)
  • issueDeviceJWT — function to sign short-lived device tokens
  • verifyDeviceJWT — function to verify tokens (used by middleware)
  • deviceJwtTtl — token expiration in seconds (default 3600)
  • emitHubEvent — event emitter for device lifecycle events
  • tenantSlug — current tenant identifier
  • isDeviceAutoApprove — auto-approve new device registrations (default false)