Back to articles
How I Handle Platform Detection and URL Validation for Multiple Video Sources in Node.js

How I Handle Platform Detection and URL Validation for Multiple Video Sources in Node.js

via Dev.to JavaScriptjohn jewski

One of the trickiest parts of building dltkk.to was handling URL validation across TikTok, YouTube and Instagram. Each platform has completely different URL structures and each needs different validation logic. Here's how I solved it. The Problem Users paste all kinds of URLs: \ https://www.tiktok.com/@user/video/123 https://vm.tiktok.com/abc123 https://youtube.com/watch?v=abc123 https://youtu.be/abc123 https://www.instagram.com/reel/abc123 https://instagram.com/p/abc123 \ \ All valid. All need different handling. Platform Detection First step is detecting which platform the URL belongs to: \ `javascript function detectPlatform(url) { if (!url || typeof url !== 'string') { return null } if (url.includes('tiktok.com')) { return 'tiktok' } if ( url.includes('youtube.com/watch') || url.includes('youtu.be/') ) { return 'youtube' } if (url.includes('instagram.com')) { return 'instagram' } return null } ` \ Simple but effective. Returns null for unsupported platforms which we handle graceful

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
1 views

Related Articles