Path, URL, and OS APIs
The path module provides platform-aware path manipulation. The URL API handles URL parsing and construction. The os module provides information about the operating system and hardware.
Use path.join() or path.resolve() rather than manually concatenating file paths so applications work correctly across operating systems.
Working with Paths and URLs
import path from 'node:path';
const filePath = path.join(process.cwd(), 'data', 'users.json');
const url = new URL('/users?id=10', '[https://example.com');
console.log(filePath);
console.log(url.searchParams.get('id')](https://example.com'%29;
console.log%28filePath%29;
console.log%28url.searchParams.get%28'id'%29));💡 The example builds a platform-aware path and reads a query parameter from a URL.
Key Takeaways
- Use
pathinstead of manual path-string concatenation. - The URL API provides structured access to URL components.
- Platform-aware APIs make applications more portable.
Knowledge Check
Which API is best suited for joining filesystem paths?