Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Node Js Module Methods

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Node Js Module Methods

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

os Module

type() :- show os name


version() :- shows os version
freemem() :- shows free space of Primary memory.
cpus() - cpu status

__dirname - current dir


__filename - current file

path module

dirname(__filename) :- it shows directory / folder name based filename what we


passed.
basename(__filename) only file name
extname(__filename) extension
parse(__filename) - file info like name, extension, foldername, folder location etc

FS module

File System

The fs (File System) module in Node.js provides an API for interacting with the
file system. It allows you to perform operations such as reading, writing,
updating, and deleting files and directories, which are essential for server-side
applications and scripts.

readFile() :- this method is used to read data from specified file.

fsMod.readFile("filename", "utf8" (err, data)=>{


if(err)
{
action
}

print "data"
})

err :- it will check file existancy, if not exist,it return false.


data:- it holds entire file info.
utf8 :- generally readFile() method reads data in binary format, this utf8 will
convert that into
string format.
writeFile() : this method is used to write content into the file.

fsMod.writeFile("filename", "content", (err)=>{


if(err)
{
action
}
});

Note :- if file already exist, that file will replaced.

rename() :- this method allows to rename a file.

fsMod.rename("old file name", "new file name", (err)=>{


if(err)
{
action
}
})

unlink() :- this method is used to delete/folder a file.

fsMode.unlink("filename", (err)=>{
if(err)
{
action
}
});

------------
appendFile() :- this method is used to add new content to the existing file.

fsMod.appendFile("existing filename", "content", (err)=>{


if(err)
{
action
}
});
-------------
close() :- this method is used to close file which is opened.
fs.close("fd", (err)=>{
if(err)
action-1
else
action-2
});
-------------
Open a File
The fs.open() method is used to create, read, or write a file. The fs.readFile()
method is only for reading the file and fs.writeFile() method is only for writing
to the file, whereas fs.open() method does several operations on a file. First, we
need to load the fs class which is a module to access the physical file system.

fs.open(path, flags, mode, callback)

Parameters:

path: It holds the name of the file to read or the entire path if stored at other
locations.
flags: Flags indicate the behavior of the file to be opened. All possible values
are ( r, r+, rs, rs+, w, wx, w+, wx+, a, ax, a+, ax+).
mode: Sets the mode of file i.e. r-read, w-write, r+ -readwrite. It sets to default
as readwrite.
err: If any error occurs.
data: Contents of the file. It is called after the open operation is executed.
------------------------------------------------------
exists() :- this method allows to check weather a file / folder is exist or not.

fsMod.exists(filename, (info)=>{
based on info check file
});
-----------------------
prompt-sync :- this module is used to read data from keyboard. it is a third party
module so that it has to install it.
npm install prompt-sync

once it is installed, we have to import as follows


const prompt = require('prompt-sync')();
--------------------
mkdir():- method in Node.js is used to create a directory asynchronously.

fsMod.mkdir(foldername, (err)=>
{
action
});

------------

You might also like