Server Configuration
Reference: Deno.serve API Documentation
Configure your Deserve server with hostname binding and graceful shutdown.
Basic Server Setup
The simplest way to start a server:
typescript
// 1. Import Router
import { Router } from '@neabyte/deserve'
// 2. Create router
const router = new Router()
// 3. Start server on port 8000 (bind 0.0.0.0)
await router.serve(8000)This starts your server on 0.0.0.0:8000 (all interfaces).
Enhanced Serve Method
Deserve's enhanced serve method supports three parameters:
typescript
// Method signatures
async serve(port: number): Promise<void>
async serve(port: number, hostname?: string): Promise<void>
async serve(port: number, hostname?: string, signal?: AbortSignal): Promise<void>Hostname Binding
Bind to Specific Interface
typescript
// Bind to localhost only
await router.serve(8000, '127.0.0.1')
// Bind to all interfaces (default)
await router.serve(8000, '0.0.0.0')
// Bind to specific network interface
await router.serve(8000, '192.168.1.100')Development vs Production
typescript
// Development - localhost only
await router.serve(8000, '127.0.0.1')
// Production - all interfaces
await router.serve(8000, '0.0.0.0')Request Timeout
You can set a request timeout when creating the router. If middleware and route handler do not finish within that time, the server responds with 503 Service Unavailable:
typescript
const router = new Router({
requestTimeoutMs: 30_000
})
await router.serve(8000)Omit requestTimeoutMs for no timeout (default).
Graceful Shutdown
Use AbortSignal for graceful server shutdown:
typescript
import { Router } from '@neabyte/deserve'
const router = new Router()
const ac = new AbortController()
await router.serve(8000, '127.0.0.1', ac.signal)
ac.abort()Process Signal Handling
typescript
// 1. Create router and AbortController
import { Router } from '@neabyte/deserve'
const router = new Router()
const ac = new AbortController()
// 2. Register signal handlers
Deno.addSignalListener('SIGINT', async () => {
ac.abort()
Deno.exit(0)
})
Deno.addSignalListener('SIGTERM', async () => {
ac.abort()
Deno.exit(0)
})
// 3. Start server with signal; call ac.abort() to shutdown
await router.serve(8000, '127.0.0.1', ac.signal)Testing Configuration
Test Basic Server
bash
# Start server
deno run --allow-net --allow-read main.ts
# Test endpoint
curl http://localhost:8000Test Hostname Binding
bash
# Bind to localhost only
deno run --allow-net --allow-read main.ts
# Should work
curl http://127.0.0.1:8000
# Should fail (if binding to 127.0.0.1 only)
curl http://0.0.0.0:8000Test Graceful Shutdown
bash
# Start server
deno run --allow-net --allow-read main.ts
# Send SIGINT (Ctrl+C)
# Server should shutdown gracefully