or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

examples

edge-cases.mdreal-world-scenarios.md
index.md
tile.json

nodejs.mddocs/reference/

Node.js Integration

Convert between Node.js HTTP objects and Web API Request/Response for custom server implementations.

Capabilities

Convert Request

/**
 * Convert Node.js IncomingMessage to Web API Request
 * @param opts - Options with request, base URL, and optional body size limit
 * @returns Promise resolving to Request
 */
function getRequest(opts: {
  request: IncomingMessage;
  base: string;
  bodySizeLimit?: number;
}): Promise<Request>;

Usage:

import { getRequest } from '@sveltejs/kit/node';
import http from 'node:http';

const server = http.createServer(async (req, res) => {
  const request = await getRequest({
    request: req,
    base: 'http://localhost:3000',
    bodySizeLimit: 10 * 1024 * 1024 // 10MB
  });

  // Use Web API Request
  const body = await request.json();
});

Convert Response

/**
 * Convert Web API Response to Node.js ServerResponse
 * @param res - Node.js ServerResponse
 * @param response - Web API Response
 * @returns Promise resolving when response is written
 */
function setResponse(
  res: ServerResponse,
  response: Response
): Promise<void>;

Usage:

import { setResponse } from '@sveltejs/kit/node';
import http from 'node:http';

const server = http.createServer(async (req, res) => {
  const response = new Response('Hello', {
    status: 200,
    headers: { 'Content-Type': 'text/plain' }
  });

  await setResponse(res, response);
});

Create Readable Stream

/**
 * Create ReadableStream from file path
 * @param file - Path to file
 * @returns ReadableStream of file contents
 * @since 2.4.0
 */
function createReadableStream(file: string): ReadableStream;

Usage:

import { createReadableStream } from '@sveltejs/kit/node';

export async function GET() {
  const stream = createReadableStream('/path/to/file.txt');

  return new Response(stream, {
    headers: {
      'Content-Type': 'text/plain',
      'Content-Disposition': 'attachment; filename="file.txt"'
    }
  });
}

Notes

  • Import from @sveltejs/kit/node
  • Used in custom Node.js server implementations
  • getRequest() converts Node req → Web Request
  • setResponse() converts Web Response → Node res
  • createReadableStream() streams files efficiently
  • bodySizeLimit in bytes (default: unlimited)
  • Useful for:
    • Custom adapters
    • Integration with existing Node.js servers
    • Middleware compatibility