Getting Started
This guide will walk you through setting up Torrin in your project, from installation to your first upload.
Installation
Torrin is split into separate packages for modularity. Choose the packages you need:
Server Packages
Install the core server package, a framework integration, and a storage driver:
bash
npm install @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-localbash
yarn add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-localbash
pnpm add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-localbash
bun add @torrin-kit/server @torrin-kit/server-express @torrin-kit/storage-localAvailable integrations:
@torrin-kit/server-express- Express.js middleware@torrin-kit/server-nestjs- NestJS module
Available storage drivers:
@torrin-kit/storage-local- Local filesystem storage@torrin-kit/storage-s3- AWS S3 and S3-compatible storage (requires@aws-sdk/client-s3)
Client Package
bash
npm install @torrin-kit/clientbash
yarn add @torrin-kit/clientbash
pnpm add @torrin-kit/clientbash
bun add @torrin-kit/clientQuick Start
1. Set up the Server
Create an endpoint that handles upload operations:
typescript
import express from 'express';
import { createTorrinExpressRouter } from '@torrin-kit/server-express';
import { createLocalStorageDriver } from '@torrin-kit/storage-local';
import { createInMemoryStore } from '@torrin-kit/server';
const app = express();
// Important: Parse JSON bodies for upload metadata
app.use(express.json());
// Create Torrin router
const torrinRouter = createTorrinExpressRouter({
storage: createLocalStorageDriver({
baseDir: './uploads'
}),
store: createInMemoryStore(),
});
// Mount at any path you prefer
app.use('/api/uploads', torrinRouter);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});typescript
import { Module } from '@nestjs/common';
import { TorrinModule } from '@torrin-kit/server-nestjs';
import { createLocalStorageDriver } from '@torrin-kit/storage-local';
import { createInMemoryStore } from '@torrin-kit/server';
@Module({
imports: [
TorrinModule.forRoot({
storage: createLocalStorageDriver({
baseDir: './uploads'
}),
store: createInMemoryStore(),
}),
],
})
export class AppModule {}2. Set up the Client
Initialize the client pointing to your server endpoint:
typescript
import { createTorrinClient } from '@torrin-kit/client';
const torrin = createTorrinClient({
endpoint: 'http://localhost:3000/api/uploads',
});3. Upload a File
Create an upload and start the transfer:
typescript
// Get file from input
const fileInput = document.querySelector<HTMLInputElement>('#file-input');
const file = fileInput.files[0];
// Create upload
const upload = torrin.createUpload({ file });
// Listen to progress
upload.on('progress', (progress) => {
console.log(`${progress.percentage}% complete`);
console.log(`${progress.chunksCompleted}/${progress.totalChunks} chunks uploaded`);
});
// Listen to status changes
upload.on('status', (status) => {
console.log('Status:', status); // idle, uploading, paused, completed, etc.
});
// Handle errors
upload.on('error', (error) => {
console.error('Upload failed:', error.message);
});
// Start the upload
try {
const result = await upload.start();
console.log('Upload complete!');
console.log('File location:', result.location);
} catch (error) {
console.error('Upload failed:', error);
}Next Steps
Now that you have a basic upload working, explore more features:
Frontend Guides: Learn framework-specific implementations
Backend Guides: Deep dive into server configuration
Core Concepts:
- Configuration - Chunk size, concurrency, retries
- Resume & Persistence - Auto-resume after page refresh
- Error Handling - Handle failures gracefully
- Storage Drivers - Local, S3, or custom storage
API Reference:
- Client API - Complete client methods and types
- Server API - Server-side interfaces