Skip to main content

Configuration

Learn how to configure the Proxy package.

Configuration Formats

The Proxy supports two configuration formats:

  1. coreGroups (Recommended) - Group routes by monolith server
  2. cores (Legacy) - Flat list of core configurations

The coreGroups format organizes routes by the serverless-monolith server they belong to:

import { ProxyServer } from '@serverless-monolith/proxy';

const proxy = new ProxyServer({
port: 5454,
cors: true,
logRequests: true,
coreGroups: [
{
coreName: 'serverless-sst',
baseUrl: 'http://localhost:4005',
routes: [
{
name: 'scheduling-imports',
pathPrefix: '/api/http/scheduling-imports/',
timeout: 30000,
},
{
name: 'documents',
pathPrefix: '/api/http/documents/',
},
{
name: 'global',
routeByPath: {
'v2/employee': {
pathPrefix: '/api/http/employee/',
preserveMatchedPath: true,
},
'': {
pathPrefix: '/api/http/global/',
},
},
},
],
},
{
coreName: 'serverless-onboarding',
baseUrl: 'http://localhost:4006',
routes: [
{
name: 'company',
pathPrefix: '/api/http/company/',
},
{
name: 'employee',
pathPrefix: '/api/http/employee/',
},
],
},
],
});

CoreGroup Options

OptionTypeRequiredDescription
coreNamestringName of the monolith server (e.g., 'serverless-sst')
baseUrlstringBase URL of the server (e.g., 'http://localhost:4005')
routesRouteConfig[]Array of route configurations

Route Options (within CoreGroup)

OptionTypeRequiredDescription
namestringRoute name (becomes the path: /name/*)
pathPrefixstringPath prefix to add to backend URL
timeoutnumberRequest timeout in milliseconds
headersobjectCustom headers to add to requests
routeByPathobjectAdvanced path-based sub-routing
aliasesstring[]Alternative route names

RouteByPath within CoreGroup

When using routeByPath inside a CoreGroup route, paths are relative to the baseUrl:

{
coreName: 'serverless-sst',
baseUrl: 'http://localhost:4005',
routes: [
{
name: 'global',
routeByPath: {
'v2/employee': {
pathPrefix: '/api/http/employee/', // Relative to baseUrl
preserveMatchedPath: true,
},
'company-exam': {
pathPrefix: '/api/http/company-exam-applications/',
fallbackUrls: ['http://localhost:4006/api/http/company-exam/'],
},
'': {
pathPrefix: '/api/http/global/', // Catch-all
},
},
},
],
}

Complete CoreGroup Example

const proxy = new ProxyServer({
port: 5454,
cors: true,
defaultTimeout: 30000,
coreGroups: [
{
coreName: 'serverless-sst',
baseUrl: 'http://localhost:4005',
routes: [
// Simple route
{
name: 'scheduling-imports',
pathPrefix: '/api/http/scheduling-imports/',
},
// Route with aliases
{
name: 'documents',
aliases: ['docs', 'files'],
pathPrefix: '/api/http/documents/',
},
// Complex routing
{
name: 'global',
routeByPath: {
'v2/employee': {
pathPrefix: '/api/http/employee/',
preserveMatchedPath: true,
transformPath: (path) => path.replace('/v2/', '/'),
},
'scheduling': {
pathPrefix: '/api/http/scheduling/',
timeout: 60000,
},
'': {
pathPrefix: '/api/http/global/',
},
},
},
],
},
{
coreName: 'serverless-onboarding',
baseUrl: 'http://localhost:4006',
routes: [
{
name: 'company',
pathPrefix: '/api/http/company/',
},
{
name: 'employee',
pathPrefix: '/api/http/employee/',
fallbackUrls: [
'http://localhost:4005/api/http/employee/',
],
},
],
},
],
});

Cores (Legacy Format)

The cores format is still supported but coreGroups is recommended:

const proxy = new ProxyServer({
port: 5454,
cors: true,
cores: [
{
name: 'my-service',
url: 'http://localhost:4005/api/http/',
pathPrefix: '/api',
},
],
});

Core Options

OptionTypeRequiredDescription
namestringCore identifier
urlstringBackend URL
pathPrefixstringPath prefix to match
timeoutnumberOverride default timeout
fallbackUrlsstring[]Fallback URLs on failure
routeByPathobjectAdvanced path-based routing
aliasesstring[]Alternative names
targetsCoreTarget[]Multiple targets for load balancing
loadBalanceStrategystring'round-robin', 'failover', or 'random'

Server Options

OptionTypeDefaultDescription
portnumber5454Proxy server port
corsboolean | CorsOptionstrueCORS configuration
logRequestsbooleantrueLog incoming requests
defaultTimeoutnumber30000Default request timeout (ms)
prefixstring''Global route prefix

CORS Configuration

Simple CORS

{
cors: true // Allow all origins
}

Advanced CORS

{
cors: {
origin: ['https://app.example.com', 'http://localhost:3000'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
maxAge: 86400,
}
}

SQS Configuration

Enable SQS routing through the proxy:

{
sqs: {
enabled: true,
refreshInterval: 30000, // Refresh queues every 30s
enableManagementRoutes: true, // Enable /sqs/* routes
routePrefix: '/sqs',
discoveryTimeout: 5000,
}
}

SQS Options

OptionTypeDefaultDescription
enabledbooleanfalseEnable SQS routing
refreshIntervalnumber30000Queue refresh interval (ms)
enableManagementRoutesbooleantrueEnable SQS management routes
routePrefixstring'/sqs'Prefix for SQS routes
discoveryTimeoutnumber5000Queue discovery timeout (ms)

Timeout Configuration

{
defaultTimeout: 30000, // Global default
coreGroups: [
{
coreName: 'api',
baseUrl: 'http://localhost:4005',
routes: [
{
name: 'fast-service',
pathPrefix: '/api/http/fast/',
timeout: 5000, // Override for this route
},
{
name: 'slow-service',
pathPrefix: '/api/http/slow/',
timeout: 60000, // Longer timeout
},
],
},
],
}

Environment-Based Configuration

const isDev = process.env.NODE_ENV !== 'production';

const proxy = new ProxyServer({
port: parseInt(process.env.PROXY_PORT || '5454'),
cors: isDev,
logRequests: isDev,
coreGroups: [
{
coreName: 'api',
baseUrl: process.env.API_URL || 'http://localhost:4005',
routes: [
{
name: 'main',
pathPrefix: '/api/http/',
},
],
},
],
});

Next Steps