Skip to main content

Function: serve()

serve(options): Server

Start an HTTP server.

const server = tjs.serve({
fetch(request) {
return new Response('Hello World!');
},
port: 8080,
});

A shorthand is also available when only a fetch handler is needed:

const server = tjs.serve((request) => new Response('Hello!'));

To enable HTTPS, provide TLS certificate and key as PEM strings:

const cert = new TextDecoder().decode(await tjs.readFile('cert.pem'));
const key = new TextDecoder().decode(await tjs.readFile('key.pem'));

const server = tjs.serve({
fetch(request) {
return new Response('Hello HTTPS!');
},
port: 8443,
tls: { cert, key },
});

Parameters

options

Server options or a fetch handler function.

ServeOptions | FetchHandler

Returns

Server

The server instance.