Documents / @faasjs/core / Server
Class: Server
HTTP server that loads and runs FaasJS API files from an app source root.
A Server resolves API route files on demand, caches loaded handlers, and
dispatches each request through the matching function lifecycle. It normalizes the
source root with a trailing path separator, loads the project .env, blocks unsafe
route traversal, and skips test-only route files.
Example
import { join } from 'node:path'
import { Server } from '@faasjs/core'
const server = new Server(join(process.cwd(), 'src'), {
port: 8080,
})
server.listen()
Constructors
Constructor
new Server(
root,opts?):Server
Create a server rooted at a FaasJS app source directory.
Parameters
root
string
App source root used to resolve configuration and route files.
opts?
ServerOptions = {}
Server configuration overrides.
Returns
Server
Throws
When onStart, onError, or onClose is not an async function.
Methods
close()
close():
Promise<void>
Close the server and wait for active requests to finish.
Returns
Promise<void>
Promise that resolves after sockets, requests, and transports stop.
handle()
handle(
req,res,options?):Promise<void>
Handle a single incoming HTTP request.
Parameters
req
IncomingMessage
Incoming Node.js request.
res
ServerResponse<IncomingMessage>
Node.js response writer.
options?
ServerHandlerOptions = {}
Optional request metadata and forced filepath override.
Returns
Promise<void>
Promise that resolves after the request has been handled.
listen()
listen():
Server
Start the underlying Node.js HTTP server.
Returns
Server
Underlying Node.js server instance.
Throws
When the server is already running.
middleware()
middleware(
req,res,next):Promise<void>
Express-style middleware wrapper that delegates to Server.handle.
Parameters
req
IncomingMessage
Incoming HTTP request object.
res
ServerResponse<IncomingMessage>
Server response object.
next
() => void
Callback used to continue the middleware chain when FaasJS does not handle the request.
Returns
Promise<void>
Promise that resolves when middleware processing finishes.
Properties
closed
protectedclosed:boolean=false
Tracks whether shutdown has completed. Subclasses may read this to avoid duplicate cleanup.
logger
readonlylogger:Logger
Shared server logger.
options
readonlyoptions:ServerOptions
Effective server options with defaults applied.
root
readonlyroot:string
Normalized app source root used to resolve route files.