serve()
Environment: server.
Low-level function that turns a telefunction HTTP request into an HTTP response. It's a pure function: stateless and side-effect-free. It runs in any runtime, with no adapter required.
Most apps should use
new Telefunc()instead — it's the standard server integration.
new Telefunc()has full-fledged support for Telefunc Stream, whereasserve()doesn't support the following:
- WebSocket
You can still use Telefunc Stream, but not over the WebSocket transport — Telefunc falls back to another transport instead.
- Channels on Cloudflare
You won't be able to use channels on Cloudflare at all. (Because channels need Durable Objects.)
Web Request
You can pass the web-standard Request object, and build a web-standard Response object from the result:
// Environment: server
import { serve } from 'telefunc'
// A fetch-style handler (Hono, Cloudflare Workers, Deno, Bun, ...)
async function handler(request) {
const httpResponse = await serve({ request })
// Convert httpResponse to a web-standard Response
return new Response(httpResponse.getReadableWebStream(), {
status: httpResponse.statusCode,
headers: httpResponse.headers
})
}// Environment: server
import { serve } from 'telefunc'
// A fetch-style handler (Hono, Cloudflare Workers, Deno, Bun, ...)
async function handler(request: Request): Promise<Response> {
const httpResponse = await serve({ request })
// Convert httpResponse to a web-standard Response
return new Response(httpResponse.getReadableWebStream(), {
status: httpResponse.statusCode,
headers: httpResponse.headers
})
}Node.js req
You can pass the Node.js req readable stream, and apply the result to the Node.js res object.
For example when using Express:
// Environment: server
import express from 'express'
import { serve } from 'telefunc'
const app = express()
app.all('/_telefunc', async (req, res) => {
const httpResponse = await serve({
url: req.originalUrl,
method: req.method,
readable: req,
headers: req.headers
})
res.status(httpResponse.statusCode)
for (const [name, value] of httpResponse.headers) {
res.setHeader(name, value)
}
await httpResponse.pipe(res)
})
app.listen(3000)With context
Pass a context object to make request-scoped data available inside telefunctions via getContext():
const httpResponse = await serve({
// ...
context: {
user: await getUser(request)
}
})The
contextparameter is optional — only needed if you usegetContext(). See API >getContext()> Provide.
Response object
httpResponse contains everything needed to send the response:
| Property | Type | Description |
|---|---|---|
statusCode | 200 | 400 | 403 | 422 | 500 | HTTP status code |
headers | [string, string][] | Response headers |
getReadableWebStream() | ReadableStream | Web-standard stream (Hono, Cloudflare, Deno, etc.) |
pipe(writable) | Promise<void> | Pipe to Node.js writable (Express, Fastify) |
getBody() | Promise<string> | Full body as string (awaits streaming if needed) |
err | unknown | The error thrown by your telefunction, if any (otherwise undefined) — see Guides > Error handling |