Sunday, 15 September 2024

Path module in Nodejs

 

Key Methods

Here are some of the commonly used methods in the path module:

  1. path.join([...paths]):

    • Joins all given path segments together, normalizing the resulting path.
    • Example:
      const path = require('path'); const filePath = path.join('/home', 'user', 'docs', 'file.txt'); console.log(filePath); // Outputs: /home/user/docs/file.txt
  2. path.resolve([...paths]):

    • Resolves a sequence of paths or path segments into an absolute path.
    • Example:
      const path = require('path'); const absolutePath = path.resolve('docs', 'file.txt'); console.log(absolutePath); // Outputs the absolute path to 'file.txt'
  3. path.basename(p, [ext]):

    • Returns the last portion of a path, optionally removing a file extension.
    • Example:
      const path = require('path'); const baseName = path.basename('/home/user/docs/file.txt'); console.log(baseName); // Outputs: file.txt const baseNameWithoutExt = path.basename('/home/user/docs/file.txt', '.txt'); console.log(baseNameWithoutExt); // Outputs: file
  4. path.dirname(p):

    • Returns the directory name of a path.
    • Example:
      const path = require('path'); const dirName = path.dirname('/home/user/docs/file.txt'); console.log(dirName); // Outputs: /home/user/docs
  5. path.extname(p):

    • Returns the extension of the path, including the leading dot (.).
    • Example:
      const path = require('path'); const extName = path.extname('/home/user/docs/file.txt'); console.log(extName); // Outputs: .txt
  6. path.parse(p):

    • Parses a path into an object with root, dir, base, ext, and name properties.
    • Example:
      const path = require('path'); const parsed = path.parse('/home/user/docs/file.txt'); console.log(parsed); /* Outputs: { root: '/', dir: '/home/user/docs', base: 'file.txt', ext: '.txt', name: 'file' } */
  7. path.format(p):

    • Formats a path object into a string path.
    • Example:
      const path = require('path'); const formatted = path.format({ root: '/', dir: '/home/user/docs', base: 'file.txt', ext: '.txt', name: 'file' }); console.log(formatted); // Outputs: /home/user/docs/file.txt
  8. path.normalize(p):

    • Normalizes a path, resolving .. and . segments.
    • Example:
      const path = require('path'); const normalized = path.normalize('/home/user/docs/../file.txt'); console.log(normalized); // Outputs: /home/user/file.txt
  9. path.relative(from, to):

    • Returns the relative path from from to to.
    • Example:
      const path = require('path'); const relative = path.relative('/home/user', '/home/user/docs/file.txt'); console.log(relative); // Outputs: docs/file.txt
  10. path.win32 and path.posix:

    • Provide platform-specific implementations of path operations. Use path.win32 for Windows paths and path.posix for POSIX paths.
    • Example:
      const path = require('path'); const posixPath = path.posix.join('/home', 'user', 'docs', 'file.txt'); console.log(posixPath); // Outputs: /home/user/docs/file.txt const win32Path = path.win32.join('C:\\', 'Users', 'Docs', 'file.txt'); console.log(win32Path); // Outputs: C:\Users\Docs\file.txt

Usage Examples

Constructing Paths

const path = require('path'); // Joining paths const fullPath = path.join('folder', 'subfolder', 'file.txt'); console.log(fullPath); // Outputs the joined path // Resolving paths const absolutePath = path.resolve('folder', 'file.txt'); console.log(absolutePath); // Outputs the absolute path

Parsing and Formatting Paths

const path = require('path'); // Parsing a path const parsedPath = path.parse('/home/user/file.txt'); console.log(parsedPath); // Outputs: { root: '/', dir: '/home/user', base: 'file.txt', ext: '.txt', name: 'file' } // Formatting a path object const formattedPath = path.format(parsedPath); console.log(formattedPath); // Outputs: /home/user/file.txt

Thank you.

No comments:

Post a Comment

Golang Advanced Interview Q&A