Path Module in NodeJS
By abstracting away variations in path
standards among different operating systems, such as Windows, Linux, and macOS, the path module in Node.js offers tools for working with file and directory paths. You may manage paths with this module without worrying about file extensions, separators, or concatenations. You use const path = require('path')
to include it in your files.
Resolving Absolute Paths and Manipulating Path Segments:
- path.resolve(): The absolute path of a series of path segments can be determined using the
path.resolve()
method. It turns a series of paths or parts of paths into an absolute path.Path.resolve()
will return the absolute path of the current working directory if no path segments are supplied.Resolve
will utilize the first parameter as the base for the second if a second parameter is specified. - path.join(): This function mixes two or more path segments while automatically managing platform-specific separators.
- path.normalize(): In order to provide a consistent format, the path.normalize() function, for example, resolves relative specifiers like. or.. and double slashes in file path strings.
- path.dirname(): By extracting the directory portion of a path, the filename is eliminated.
- path.basename(): The final part of a path, usually the filename, is returned by
path.basename()
. It is possible to exclude the file extension using an optional second parameter. - path.extname(): The function
path.extname()
yields the path’s extension (e.g.,.txt
,.js
). - path.parse(): The function
path.parse()
converts a path string into an object by providing elements such asroot
,dir
,base
,name
, andext
.
Additionally helpful is the __dirname
global variable, which gives the name of the directory in which the script is now running. This information enables path creation with respect to the script. No matter which working directory the script is currently running from, this is useful when you need to refer to a file related to the script.
Querystring Module
URL query string parsing and formatting tools are available in the querystring module. In URLs, query strings often follow the? character, but they can also show up when working with POST data.
- querystring.parse(): In order to return a JavaScript object, the querystring.parse() method breaks down an existing URL query string into a collection of key-value pairs. Additionally, it can handle assignment characters and custom separators.
- querystring.stringify(): On the other hand, querystring.stringify() creates a query string from a map of key-value pairs (a JavaScript object). It takes assignment and custom separator parameters, just like
parse
.
Readline Module
An interface for reading data one line at a time from a Readable stream, such process.stdin
, is offered by the readline
module. Building interactive command-line interfaces (CLIs) is one application where this is especially helpful.
- readline.createInterface(options): The
readline
module’s main function for getting started is readline.createInterface(options). AnInterface
instance is created, with the input stream (process.stdin
for user input, orfs.createReadStream
for a file) and output stream (process.stdout
, for example) defined. Usually, you would setterminal
tofalse
for files. - Reading Data Line by Line (rl.on(‘line’)): Whenever a newline character (
\r
,\n
, or\r\n
) is received by the input stream, Aline
event is emitted by theInterface
object. This enables line-by-line processing of input. - Prompting User Input (rl.question()): The
question()
method asks the user a question on theoutput
stream and then watches the input stream forinput
. The entered string is passed to thecallback
function when the user hits Enter. It’s crucial to end the interface by callingrl.close()
inside the callback.
Other events also supported by the readline
module include pause
(when the stream is halted), resume
(when a stopped stream is restarted), and close
(when the stream is closed). The underlying functionality of the readline module is also used by commands in the REPL (Read-Eval-Print-Loop), including .help
, .editor
, .break
, .clear
, .load
, .save
, and .exit
also use the readline
module’s allowing for interactive command-line experiences.
Providing strong and effective methods for managing file system interactions, parsing URL data, and developing interactive command-line tools, these utility modules are essential to Node.js development. They are made to easily connect with the asynchronous, event-driven architecture of Node.js, allowing programmers to create incredibly adaptable and powerful applications.
Readline sync
readline-sync
is a popular Node.js module that lets you create synchronous command-line interfaces (CLIs). Essentially, it allows a Node.js program to pause and wait for user input from the console. This is different from the standard Node.js readline
module, which is asynchronous and uses an event-driven approach.