Overview
The WebTorrent server creates an HTTP server to serve the contents of torrents, dynamically fetching needed pieces to satisfy requests. Range requests are supported.WebTorrent provides two server implementations:
- NodeServer for Node.js environments
- BrowserServer for browser environments (requires Service Worker)
Creating a Server
client.createServer([opts], [force])
Creates an HTTP server for serving torrent contents. See the WebTorrent API for more details on client methods.
Node.js Server
Configuration Options
Allow requests from specific origin. Set to
false for same-origin only.If specified, only allow requests whose Host header matches this hostname.
Base path for the server.
Usage Example
URL Structure
The server provides the following endpoints:Root endpoint (no content displayed)
Lists all torrents with links to their detail pages
Lists all files in the torrent with links to stream/download them
Streams or downloads the specific file
Methods
server.server.listen(port, [callback])
Start the server listening on the specified port.
server.close([callback])
Close the server and stop accepting new requests.
server.destroy([callback])
Destroy the server and close all open connections.
server.address()
Get the server’s address information.
Object with
address, family, and port propertiesBrowser Server
Configuration Options
Service worker registration
Allow requests from specific origin. Set to
false for same-origin only.Usage Example
Service Worker
You can use the official WebTorrent service worker or implement the required functionality yourself:Methods
server.close([callback])
Close the server.
server.destroy([callback])
Destroy the server.
Response Headers
All file responses include the following headers:Indicates support for range requests
MIME type of the file
Size of the response body in bytes
Present for partial content responses (206)
Disables caching (data is local)
CORS header (if origin is allowed)
CORS Support
By default, the server allows all origins (origin: '*'). You can restrict this:
Range Requests
The server supports HTTP range requests, enabling:- Video/audio seeking
- Resumable downloads
- Partial content delivery