Int222unit 2
Int222unit 2
BY : DIVYA THAKUR
UID:28300
Contents :
• SYNCHRONOUS VS ASYNCHRONOUS
• CALLBACK FUCNTION
• FS MODULE
• OS MODULE
• PATH MODULE
Synchronous VS Asynchronous
• //synchronous
const fs = require("fs");
const data = fs.readFileSync("file1.txt" , "utf-8");
console.log(data);
//Asynchronous
const fs = require("fs");
fs.readFile("file1.txt","utf-8",(err,data) => {
š console.log(data);
})
console.log("second data");
Callback
• Any Function that is passed as an arguement is called callback
function
• A callback is a function that is to be executed after another function
has finished execution - hence the name call back.
• Callback is an asynchronous equivalent for a function.
• A callback function is called at the completion of a given task. Node
makes heavy use of callbacks.
• All the APIs of Node are written in such a way that they support
callbacks.
• A callback is a function called at the completion of a given task; this
prevents any blocking, and allows other code to be run in the
meantime.
• Callbacks give you an interface with which to say, "and when you're
done doing that, do all this."
• This allows you to have as many IO operations as your OS can
handle happening at the same time.
Example of callback function
const abc = (name,callback) => {
console.log(`hello ${name}`);
callback();
}
abc("divya",abcd);
Working With FS Module
Before using any module ,we need to require it.
Various modules are as follows:
1. fs (file system): create
write
read
rename
Synchronous File System Module
1 Create a file:
Need to require the module :
const fs =require(“fs”);
fs.writeFilesync(“filename.txt”,’contetnt of file’);
5 Rename a File:
fs.renameSync("oldfilename.txt","newfilename.txt");
Asynchronous FS /Callback required
• 1 Create a file:
const fs= require("fs");
fs.writeFile("fs.txt","i am the original data",(err)=>{
console.log('file is created');
console.log(err);
});
• 2 Add Data to file:
fs.appendFile("fs.txt",": ,hello i am added ",(err) =>{
console.log("task completed");
• 3 Read File Data:
fs.readFile("fs.txt",(err,data) =>{
console.log(data);
})
Operating System Module
• The node:os module provides operating system-related utility
methods and properties. It can be accessed using
const os = require('node:os');
• Used to access data and information of the operating system using
nodejs.
• The OS module provides information about the computer's operating
system.
OS Properties and Methods
1. arch() : Returns the operating system CPU architecture
const os=require("os");
console.log(os.arch());
2. os.freemem() : Returns the amount of free system memory in bytes
as an integer.
const mem = os.freemem();
console.log(mem);
3.totalmem() :Returns the number of total memory of the system
const mem = os.totalmem();
console.log(mem);
4. hostname() : Returns the hostname of the operating system
console.log(os.hostname());
5. release() :Returns information about the operating system's
release.
console.log(os.release());
6. tmpdir() :Returns the operating system's default directory for
temporary files.
console.log(os.tmpdir());
7. type() :Returns the name of the operating system.
console.log(os.type());
8. uptime() :Returns the uptime of the operating system, in
seconds. This method returns an integer value that specifies
the number of seconds the system is running i.e. system uptime.
console.log(os.uptime());
9.userInfo() :Returns information about the current user.
console.log(os.userInfo());
10.version() : returns version of operating system .
console.log(os.version());
11. os.constants :
console.log(os.constants);
The path.delimiter property returns the delimiter specified for the server.
; (semicolon) for Windows
: (colon) for POSIX
(POSIX (Portable Operating System Interface) is a set of standard operating system
interfaces based on the Unix operating system.
6. join() : Joins the specified paths into one.
You can specify as many path segments as you like.
The specified path segments must be strings,
separated by comma.
const pjoin = path.join('ab', 'abc', 'c_path.js');
console.log(x);