LogoRobo.js

API reference

Full reference for Server, RoboRequest, RoboReply, RoboResponse, and utility functions.

Import types and utilities from the main package:

import { Server, RoboResponse, define, getServerEngine, ready } from '@robojs/server'
import type { RoboRequest, RoboReply, HttpMethod } from '@robojs/server'
import { Server, RoboResponse, define, getServerEngine, ready } from '@robojs/server'

Server

The Server object provides access to the server configuration, engine, and readiness state.

import { Server } from '@robojs/server'
import { Server } from '@robojs/server'
MethodReturn TypeDescription
Server.config()PluginConfigReturns the resolved plugin configuration.
Server.get()BaseEngine | undefinedReturns the current server engine instance.
Server.ready()Promise<void>Resolves when the server is ready to accept requests.
import { Server } from '@robojs/server'

// Wait for the server to be ready
await Server.ready()

// Access the engine
const engine = Server.get()
const httpServer = engine?.getHttpServer()

// Read config
const { port, hostname } = Server.config()
import { Server } from '@robojs/server'

// Wait for the server to be ready
await Server.ready()

// Access the engine
const engine = Server.get()
const httpServer = engine?.getHttpServer()

// Read config
const { port, hostname } = Server.config()

RoboRequest

RoboRequest extends the Web Request API with additional properties for handling route parameters and query strings.

Property/MethodTypeDescription
paramsRecord<string, string>URL parameters from dynamic route segments.
queryRecord<string, string | string[]>Parsed query string parameters.
rawIncomingMessageThe underlying Node.js request object.
json()Promise<unknown>Parse the request body as JSON. Inherited from Request.
methodstringThe HTTP method (GET, POST, etc.).
urlstringThe full request URL.
headersHeadersRequest headers.
import type { RoboRequest } from '@robojs/server'

export async function POST(request: RoboRequest) {
	const { id } = request.params
	const { page } = request.query
	const body = await request.json()
	const raw = request.raw // Node.js IncomingMessage

	return { id, page, body }
}

export async function POST(request) {
	const { id } = request.params
	const { page } = request.query
	const body = await request.json()
	const raw = request.raw // Node.js IncomingMessage

	return { id, page, body }
}

request.query returns only the first value per key. For multi-value query parameters, use new URL(request.url).searchParams directly.

RoboReply

RoboReply wraps the Node.js ServerResponse with a chainable API.

Property/MethodTypeDescription
rawServerResponseThe underlying Node.js response object.
hasSentbooleanWhether the response has already been sent.
code(statusCode)RoboReplySet the HTTP status code. Returns this for chaining.
header(name, value)RoboReplySet a response header. Returns this for chaining.
json(data)RoboReplySend a JSON response. Returns this for chaining.
send(data)RoboReplySend the response body. Returns this for chaining.
import type { RoboRequest, RoboReply } from '@robojs/server'

export function GET(request: RoboRequest, reply: RoboReply) {
	reply.code(200).header('X-Custom', 'value').json({ message: 'Hello' })
}

export function GET(request, reply) {
	reply.code(200).header('X-Custom', 'value').json({ message: 'Hello' })
}

Most of the time you don't need reply — just return a value from your handler and it's automatically sent as a response.

RoboResponse

RoboResponse extends the Web Response API. Use it when you need to set custom status codes or headers.

import { RoboResponse } from '@robojs/server'

// JSON response with custom status
export function POST() {
	return RoboResponse.json({ id: '123' }, { status: 201 })
}

// Throwable response for error handling
export function GET(request: RoboRequest) {
	if (!request.query.token) {
		throw RoboResponse.json({ error: 'Token required' }, { status: 401 })
	}
	return { data: 'authenticated' }
}
import { RoboResponse } from '@robojs/server'

// JSON response with custom status
export function POST() {
	return RoboResponse.json({ id: '123' }, { status: 201 })
}

// Throwable response for error handling
export function GET(request) {
	if (!request.query.token) {
		throw RoboResponse.json({ error: 'Token required' }, { status: 401 })
	}
	return { data: 'authenticated' }
}

Utility functions

getServerEngine

Returns the server engine instance with optional type parameter:

import { getServerEngine } from '@robojs/server'

const engine = getServerEngine()
const httpServer = engine.getHttpServer()
import { getServerEngine } from '@robojs/server'

const engine = getServerEngine()
const httpServer = engine.getHttpServer()

ready

Standalone function equivalent to Server.ready():

import { ready } from '@robojs/server'

await ready()
import { ready } from '@robojs/server'

await ready()

isPortAvailable

Check if a port is available:

import { isPortAvailable } from '@robojs/server'

const available = await isPortAvailable(3000)
import { isPortAvailable } from '@robojs/server'

const available = await isPortAvailable(3000)

findAvailablePort

Find the next available port:

import { findAvailablePort } from '@robojs/server'

const result = await findAvailablePort({
	port: 3000,
	hostname: 'localhost',
	maxAttempts: 10
})
// result: { port: 3001, wasIncremented: true, originalPort: 3000 }
import { findAvailablePort } from '@robojs/server'

const result = await findAvailablePort({
	port: 3000,
	hostname: 'localhost',
	maxAttempts: 10
})
// result: { port: 3001, wasIncremented: true, originalPort: 3000 }

define

Create a typed endpoint with Zod schemas. See Typed endpoints.

Types

import type {
	RoboRequest,
	RoboReply,
	HttpMethod,
	RouteHandler,
	NotFoundHandler,
	WebSocketHandler,
	ForTestingOptions,
	FindAvailablePortOptions,
	FindAvailablePortResult,
	TunnelProvider,
	TunnelInstance,
	TunnelProviderConfig,
	TunnelConfig
} from '@robojs/server'
TypeDescription
HttpMethod'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD'
RouteHandler(req: RoboRequest, res: RoboReply) => unknown | Promise<unknown>
NotFoundHandlerSame as RouteHandler. Used for custom 404 handling.
WebSocketHandler(req: IncomingMessage, socket: Duplex, head: Buffer) => void
ForTestingOptionsOptions for RoboRequest.forTesting()
TunnelProviderInterface for custom tunnel providers
TunnelInstanceTunnel process reference with URL
TunnelConfigTunnel configuration options

Next steps

On this page