Commonly used functions in path
Today we learn Commonly used functions in path
Two commonly used functions in the path module are path.join() and path.resolve()
These two functions deal with segments starting with / in very different ways
The path.join and path.resolve are two different methods of the path module.
Both these methods accept a sequence of paths or path segments.
The path.resolve() method resolves a sequence of paths or path segments into an absolute path.
The path.join() method joins all given path segments together using the platform specific separator as a delimiter, then normalizes the resulting path.
In order to better understand and differentiate behaviours, let me explain it with different scenarios.
1 | path.join('/a', '/b') // Outputs '/a/b' |
Introduction to path.join()
The path.join function is used to concatenate multiple path segments together. It results a single normalized path string. It handles the differences in file path separators between operating systems (e.g., forward slash / on Unix-like systems and backward slash \ on Windows) and ensures the correct joining of paths.
Example of path.join
1 | const path = require('path'); // |
Introduction to path.resolve()
The path.resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created. The resulting path is normalized and trailing slashes are removed as required.
If no path segments are given as parameters, then the absolute path of the current working directory is used.
Example of path.resolve()
1 | const path = require('path'); |
One important characteristic of path.resolve is that it always returns an absolute path. If the resolved path is already an absolute path, it is returned as is. If the resolved path is a relative path, it is resolved relative to the current working directory.
Hello World
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.
Quick Start
Create a new post
1 | $ hexo new "My New Post" |
More info: Writing
Run server
1 | $ hexo server |
More info: Server
Generate static files
1 | $ hexo generate |
More info: Generating
Deploy to remote sites
1 | $ hexo deploy |
More info: Deployment