A high-performance, secure file upload server designed for ShareX with enhanced features and optimizations.
- Async/Await Consistency: All file operations use modern async/await patterns
- Memory Optimization: V8 memory flags optimized for large file uploads
- Stream Processing: Efficient streaming for large file uploads with progress tracking
- Caching: Static file caching with appropriate cache headers
- Connection Management: Optimized timeouts and keep-alive settings
- Path Traversal Protection: Comprehensive filename validation and sanitization
- Rate Limiting: Built-in rate limiting for upload and delete endpoints
- Security Headers: Comprehensive security headers (XSS, CSRF, etc.)
- Input Validation: Enhanced validation for all user inputs
- API Key Security: Improved key validation with length checks
- Error Handling: Comprehensive error handling with proper cleanup
- Type Safety: Better parameter validation and type checking
- Code Organization: Modular structure with utility functions
- Documentation: Extensive JSDoc documentation
- Constants: Extracted constants for better maintainability
- WebSocket Support: Real-time upload progress tracking
- Enhanced Logging: Detailed logging with performance metrics
- Graceful Shutdown: Proper cleanup on server shutdown
- File Metadata: Extended file information in responses
- Health Monitoring: Better error tracking and monitoring
- Node.js 14.x or higher
- npm or yarn package manager
- Clone the repository:
git clone https://github.com/Ryahn/node-sharex-server.git
cd node-sharex-server
- Install dependencies:
npm install
- Copy and configure the config file:
cp config-example.json config.json
- Edit
config.json
with your settings:
{
"port": 3854,
"name": "YourServerName",
"keys": {
"username": "your-secure-api-key-here"
},
"fileSizeLimit": 59999999,
"largeFileSizeLimit": 5368709120,
"fileNameLength": 8,
"useLocalStaticServe": true,
"useFileIndex": true,
"staticFileServerUrl": "https://your-domain.com/f/",
"serverUrl": "https://your-domain.com",
"uploadDirectory": "uploads/",
"ssl": {
"useSSL": false,
"privateKeyPath": "",
"certificatePath": ""
},
"fileExtensionCheck": {
"enabled": true,
"extensionsAllowed": [
".png", ".jpg", ".jpeg", ".gif", ".mkv",
".mp4", ".m4v", ".webm", ".avif", ".webp"
]
}
}
npm start
The server will start and display:
- Server version and port
- Upload directory location
- Maximum file size
- SSL status
- Server URL
POST /upload
Content-Type: multipart/form-data
Parameters:
- file: The file to upload
- key: Your API key
GET /f/:filename
GET /delete?filename=:filename&key=:key
GET /config?key=:key
- Visit
/config?key=your-api-key
in your browser - Download the
.sxcu
configuration file - Import it into ShareX
fileSizeLimit
: Maximum file size for regular uploads (bytes)largeFileSizeLimit
: Maximum file size for streaming uploads (bytes)fileNameLength
: Length of random string in generated filenamesuploadDirectory
: Directory to store uploaded files
fileExtensionCheck.enabled
: Enable/disable file extension validationfileExtensionCheck.extensionsAllowed
: Array of allowed file extensionskeys
: Object mapping usernames to API keys
port
: Server portname
: Server name (used in ShareX config)serverUrl
: Public server URLstaticFileServerUrl
: URL for serving uploaded filesuseLocalStaticServe
: Serve files locally vs external CDNuseFileIndex
: Enable file listing interface
ssl.useSSL
: Enable HTTPSssl.privateKeyPath
: Path to SSL private keyssl.certificatePath
: Path to SSL certificate
- Upload endpoint: 50 requests per 15 minutes
- Delete endpoint: 100 requests per 15 minutes
- Configurable per-user limits
- Extension whitelist/blacklist
- MIME type validation
- File size limits
- Path traversal prevention
- X-Content-Type-Options: nosniff
- X-Frame-Options: DENY
- X-XSS-Protection: 1; mode=block
- Referrer-Policy: strict-origin-when-cross-origin
- Streaming uploads for files > regular limit
- Progress tracking via WebSocket
- Memory-efficient processing
- Automatic cleanup on errors
- Static file caching (1 day for CSS/JS, 1 week for images)
- ETag support
- Last-Modified headers
- Cache-Control headers
- 1-hour timeout for large uploads
- Keep-alive optimization
- Graceful shutdown handling
- Connection pooling
Build and run with Docker:
docker build -t sharex-server .
docker run -p 3854:3854 -v $(pwd)/uploads:/app/uploads sharex-server
Or use Docker Compose:
docker-compose up -d
All API responses follow a consistent format:
{
"success": true,
"message": "File uploaded successfully",
"data": {
"file": {
"url": "https://your-domain.com/f/filename.ext",
"delete_url": "https://your-domain.com/delete?filename=filename.ext&key=your-key"
}
},
"timestamp": "2023-01-01T00:00:00.000Z"
}
{
"success": false,
"error": {
"message": "Invalid file extension",
"code": "INVALID_EXTENSION",
"fix": "Upload a file with an allowed extension",
"timestamp": "2023-01-01T00:00:00.000Z"
}
}
βββ app.js # Main application file
βββ config.json # Configuration file
βββ package.json # Dependencies and scripts
βββ routes/
β βββ index.js # Main routes (upload, delete, config)
β βββ fileIndex.js # File listing routes
βββ libs/
β βββ middleware.js # Authentication and security middleware
β βββ response.js # Response utilities
β βββ handleLargeFile.js # Large file upload handler
β βββ utils.js # Common utility functions
βββ views/ # Handlebars templates
βββ public/ # Static assets
βββ uploads/ # Upload directory
- Create utility functions in
libs/utils.js
- Add middleware in
libs/middleware.js
- Create routes in
routes/
- Use consistent error handling with
libs/response.js
Code | Description | HTTP Status |
---|---|---|
EMPTY_KEY | API key is required | 400 |
INVALID_KEY | Invalid API key | 401 |
NO_FILE | No file was uploaded | 400 |
INVALID_EXTENSION | Invalid file extension | 400 |
FILE_TOO_LARGE | File exceeds size limit | 413 |
FILE_NOT_FOUND | File not found | 404 |
RATE_LIMITED | Too many requests | 429 |
SERVER_ERROR | Internal server error | 500 |
The server provides detailed logging for:
- Authentication attempts
- File uploads/downloads
- Error conditions
- Performance metrics
- Security events
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Original ShareX upload server by ravi0lii
- Enhanced and optimized by Ryahn
- Community contributions and feedback
- GitHub Issues: Report bugs or request features
- Documentation: Check this README and inline code documentation
- Community: Join discussions in GitHub Discussions