Node.js path Module
The path module is a built-in module to provide several utilities to access and interact with the file system. This module provides a way of working with directories and file paths.
There is no need to install the "path" module externally, as it is the part of the Node.js core.
var path = require('path');
The path module working and operations vary according to the underlying operating system on which a Node.js application is running. Especially, while the Windows operating system is being used, the path module will assume that Windows-style paths are being used and the results will be different on POSIX and Windows.
path.basename(path[, ext])
This method returns the last part of a path. The "ext" is the optional file extension.
var path = require('path');
console.log(path.basename("C:\\temp\\index.html"));
//without extension
console.log(path.basename("C:\\temp\\index.html",".html"));//with extension
/*
Output:
index.html
index
*/
path.delimiter
provides the delimiter platform-specific delimiter ; for Windows and : for POSIX.
var path = require('path');
console.log(path.delimiter);
/*Output:
;
*/
path.dirname(path)
This method returns the directory name of a specified path.
var path = require('path');
console.log(path.dirname("foo/bar/mydir/test.js"));
/*Output:
foo/bar/mydir
*/
path.extname(path)
This method returns the file extension of a path.
var path = require('path');
console.log(path.extname("foo/bar/mydir/index.html"));
/*Output:
.html
*/
path.format(pathObject)
This method is used to formats a path object into a path string.
var path = require('path');
console.log(path.format({
dir: 'C:\\foo\\bar\\dir',
base: 'message.txt'
}));
/*
Output:C:\foo\bar\dir\message.txt
*/
path.isAbsolute(path)
This method returns true if a path is an absolute path, else it will return false
*/
var path = require('path');
console.log(path.isAbsolute("C:\\temp\\foo\\bar"));
console.log(path.isAbsolute("index.html"));
/*
Output:
true
false*/
path.join([...paths])
This method is used to join the provided paths into one.
var path = require('path');
console.log(path.join('/foo', 'bar', 'dir/test','..' ,'index.html'));
/*
Output:
\foo\bar\dir\index.html
*/
Path.normalize(path)
This method is used to normalizes the given path, by resolving '..' and '.' segments.
var path = require('path');
console.log(path.normalize('C:\\temp\\\\foo\\bar\\..\\'));
/*
Output:
C:\temp\foo\
*/
path.parse(path)
This method (returns an object that represents path properties) is used to formats a path string into a path object platform specifically.
var path = require('path');
console.log("path object->")
console.log(path.parse('C:\\path\\dir\\message.txt'));
/*
Output:
path
object->
{
root: 'C:\\',
dir: 'C:\\path\\dir',
base: 'message.txt',
ext: '.txt',
name: 'message'
}
*/
path.relative(from, to)
This method returns the relative path from one specified path to another specified path based on the working directory.
var path = require('path');
console.log(path.relative('C:\\foo\\bar\\aaa', 'C:\\foo\\bar\\bbb'));
/*
Output:
..\bbb
*/
path.resolve([...paths])
This method takes a sequence of paths as input and resolves the specified paths into an absolute path.
var path = require('path');
console.log(path.resolve('/foo/bar', './test'));
console.log(path.resolve('/foo/bar', '/temp/test/'));
/*
Output:
C:\foo\bar\test
C:\temp\test
*/
path.sep
provides the segment separator specified for the underlying platform
var path = require('path');
console.log(path.sep);
/*
Output:
\
*/
win32
Returns an object containing Win32 specific properties and path methods.
Reference:https://nodejs.org/api/path.html