Server Creation
This reference covers the APIs for creating and configuring servers in Verb.
createServer(protocol?)
Creates a new server instance with optional protocol specification.
Signature
function createServer(protocol?: ServerProtocol): UnifiedServerInstance
Parameters
protocol
(optional): The protocol to use for the server. Defaults toServerProtocol.HTTP
.
Returns
Returns a unified server instance that implements the appropriate server interface based on the protocol.
Example
import { createServer, ServerProtocol } from "verb";
// Default HTTP server (explicit)
const app = createServer(ServerProtocol.HTTP);
// Explicit HTTP server
const httpApp = createServer(ServerProtocol.HTTP);
// HTTPS server
const httpsApp = createServer(ServerProtocol.HTTPS);
// WebSocket server
const wsApp = createServer(ServerProtocol.WEBSOCKET);
createUnifiedServer(protocol?)
Alternative API for creating servers, identical to createServer()
.
Signature
function createUnifiedServer(protocol?: ServerProtocol): UnifiedServerInstance
Parameters
protocol
(optional): The protocol to use for the server. Defaults toServerProtocol.HTTP
.
Returns
Returns a unified server instance.
Example
import { createUnifiedServer, ServerProtocol } from "verb";
const app = createUnifiedServer(ServerProtocol.HTTP2);
server
Object
Fluent API for creating servers with a more readable syntax.
Properties
All methods return a server instance configured for the specified protocol.
server.http()
Creates an HTTP server.
const app = server.http();
server.https()
Creates an HTTPS server.
const app = server.https();
server.http2()
Creates an HTTP/2 server.
const app = server.http2();
server.http2s()
Creates an HTTP/2 Secure server.
const app = server.http2s();
server.websocket()
Creates a WebSocket server.
const app = server.websocket();
server.websockets()
Creates a WebSocket Secure server.
const app = server.websockets();
server.grpc()
Creates a gRPC server.
const app = server.grpc();
server.grpcs()
Creates a gRPC Secure server.
const app = server.grpcs();
server.udp()
Creates a UDP server.
const app = server.udp();
server.dtls()
Creates a DTLS (secure UDP) server.
const app = server.dtls();
server.tcp()
Creates a TCP server.
const app = server.tcp();
server.tls()
Creates a TLS (secure TCP) server.
const app = server.tls();
server.gateway(defaultProtocol?)
Creates a protocol gateway.
const gateway = server.gateway();
const httpsGateway = server.gateway(ServerProtocol.HTTPS);
server.unified(protocol?)
Creates a unified server (alias for createServer
).
const app = server.unified(ServerProtocol.HTTP2);
ServerProtocol
Enum
Enum defining all supported protocols.
Values
enum ServerProtocol {
HTTP = "http",
HTTPS = "https",
HTTP2 = "http2",
HTTP2S = "http2s",
WEBSOCKET = "websocket",
WEBSOCKETS = "websockets",
GRPC = "grpc",
GRPCS = "grpcs",
UDP = "udp",
DTLS = "dtls",
TCP = "tcp",
TLS = "tls"
}
Protocol Groups
HTTP-based Protocols
HTTP
- Standard HTTP/1.1HTTPS
- HTTP/1.1 with TLSHTTP2
- HTTP/2 without TLSHTTP2S
- HTTP/2 with TLS
WebSocket Protocols
WEBSOCKET
- WebSocket without TLSWEBSOCKETS
- WebSocket with TLS (WSS)
RPC Protocols
GRPC
- gRPC without TLSGRPCS
- gRPC with TLS
Connection-oriented Protocols
TCP
- TCP without TLSTLS
- TCP with TLS
Connectionless Protocols
UDP
- UDP without TLSDTLS
- UDP with TLS
UnifiedServerInstance
Type
The unified server instance type that adapts based on the protocol.
Type Definition
type UnifiedServerInstance =
| HttpServerInstance
| HttpsServerInstance
| Http2ServerInstance
| Http2sServerInstance
| WebSocketServerInstance
| WebSocketsServerInstance
| GrpcServerInstance
| GrpcsServerInstance
| UdpServerInstance
| DtlsServerInstance
| TcpServerInstance
| TlsServerInstance;
Common Interface
All server instances share some common methods:
interface BaseServerInstance {
listen(port?: number, hostname?: string): any;
withOptions(options: ListenOptions): void;
}
HTTP-based Server Interface
HTTP-based servers (HTTP, HTTPS, HTTP2, HTTP2S, WebSocket, WebSockets) implement:
interface HttpServerInstance extends BaseServerInstance {
// HTTP Methods
get(path: string, ...handlers: MiddlewareHandler[]): void;
post(path: string, ...handlers: MiddlewareHandler[]): void;
put(path: string, ...handlers: MiddlewareHandler[]): void;
delete(path: string, ...handlers: MiddlewareHandler[]): void;
patch(path: string, ...handlers: MiddlewareHandler[]): void;
head(path: string, ...handlers: MiddlewareHandler[]): void;
options(path: string, ...handlers: MiddlewareHandler[]): void;
// Middleware
use(middleware: Middleware): void;
use(path: string, ...middlewares: Middleware[]): void;
// Routing
route(path: string): RouteInstance;
// Bun Native Routes
withRoutes(routes: RouteConfig): void;
// Testing
createFetchHandler(): (req: Request) => Promise<Response>;
// Application Configuration
set(key: string, value: any): void;
getSetting(key: string): any;
locals: Record<string, any>;
mountpath: string;
path(): string;
}
Configuration
ListenOptions
Options for server configuration.
interface ListenOptions {
port?: number;
hostname?: string;
development?: DevelopmentOptions;
showRoutes?: boolean;
}
DevelopmentOptions
Development-specific options.
interface DevelopmentOptions {
hmr?: boolean; // Hot module reloading
console?: boolean; // Enhanced console logging
}
Example Configuration
const app = createServer(ServerProtocol.HTTP);
app.withOptions({
port: 3000,
hostname: "localhost",
showRoutes: true,
development: {
hmr: true,
console: true
}
});
app.listen();
Server Methods
listen(port?, hostname?)
Starts the server on the specified port and hostname.
// Use configured options
app.listen();
// Override port
app.listen(3000);
// Override port and hostname
app.listen(3000, "0.0.0.0");
withOptions(options)
Configures server options.
app.withOptions({
port: 3000,
hostname: "localhost",
showRoutes: true
});
withRoutes(routes)
(HTTP-based servers only)
Configures Bun native routes.
app.withRoutes({
"/": new Response("Hello World"),
"/api/users": {
GET: () => Response.json({ users: [] })
}
});
Application Configuration
set(key, value)
Sets an application setting.
app.set("trust proxy", true);
app.set("view engine", "ejs");
getSetting(key)
Gets an application setting.
const env = app.getSetting("env");
const trustProxy = app.getSetting("trust proxy");
locals
Application-wide variables.
app.locals.title = "My App";
app.locals.version = "1.0.0";
// Access in routes
app.get("/", (req, res) => {
res.json({ title: app.locals.title });
});
mountpath
The path at which the server is mounted.
console.log(app.mountpath); // "/"
path()
Returns the canonical path of the server.
console.log(app.path()); // "/"
Error Handling
Server Creation Errors
try {
const app = createServer(ServerProtocol.HTTP);
} catch (error) {
console.error("Failed to create server:", error);
}
Listen Errors
try {
app.listen(3000);
} catch (error) {
console.error("Failed to start server:", error);
}
Examples
Basic HTTP Server
import { createServer, ServerProtocol } from "verb";
const app = createServer(ServerProtocol.HTTP);
app.get("/", (req, res) => {
res.json({ message: "Hello World!" });
});
app.listen(3000);
HTTPS Server
import { createServer, ServerProtocol } from "verb";
const app = createServer(ServerProtocol.HTTPS);
app.withOptions({
port: 443,
hostname: "localhost"
});
app.get("/", (req, res) => {
res.json({ secure: true });
});
app.listen();
WebSocket Server
import { createServer, ServerProtocol } from "verb";
const app = createServer(ServerProtocol.WEBSOCKET);
app.websocket({
open: (ws) => {
ws.send("Connected to WebSocket server");
},
message: (ws, message) => {
ws.send(`Echo: ${message}`);
}
});
app.listen(3001);
Multiple Servers
import { createServer, ServerProtocol } from "verb";
// HTTP server
const httpApp = createServer(ServerProtocol.HTTP);
httpApp.get("/", (req, res) => res.json({ protocol: "http" }));
httpApp.listen(3000);
// WebSocket server
const wsApp = createServer(ServerProtocol.WEBSOCKET);
wsApp.websocket({
open: (ws) => ws.send("WebSocket connected")
});
wsApp.listen(3001);
// gRPC server
const grpcApp = createServer(ServerProtocol.GRPC);
grpcApp.addMethod("TestService", {
name: "Test",
handler: async () => ({ success: true })
});
grpcApp.listen(50051);
Fluent API
import { server } from "verb";
// Create multiple servers with fluent API
const httpApp = server.http();
const httpsApp = server.https();
const wsApp = server.websocket();
const grpcApp = server.grpc();
// Configure and start
httpApp.get("/", (req, res) => res.json({ type: "http" }));
httpApp.listen(3000);
httpsApp.get("/", (req, res) => res.json({ type: "https" }));
httpsApp.listen(443);
wsApp.websocket({
open: (ws) => ws.send("WebSocket ready")
});
wsApp.listen(3001);
grpcApp.addMethod("Service", {
name: "Method",
handler: async () => ({ result: "success" })
});
grpcApp.listen(50051);
See Also
- Protocol Gateway - Runtime protocol switching
- HTTP Server - HTTP server specifics
- WebSocket Server - WebSocket server specifics
- gRPC Server - gRPC server specifics