0% found this document useful (0 votes)
35 views

Int222unit 2

The document provides information on synchronous vs asynchronous operations, callback functions, and common Node.js modules - fs, os, and path. It defines synchronous and asynchronous operations, how callbacks allow asynchronous code to run, and gives examples of reading, writing, and modifying files with the fs module. It also outlines various methods and properties to get operating system information with the os module and work with file paths using the path module.

Uploaded by

Riya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Int222unit 2

The document provides information on synchronous vs asynchronous operations, callback functions, and common Node.js modules - fs, os, and path. It defines synchronous and asynchronous operations, how callbacks allow asynchronous code to run, and gives examples of reading, writing, and modifying files with the fs module. It also outlines various methods and properties to get operating system information with the os module and work with file paths using the path module.

Uploaded by

Riya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

INT222

Advance Web Development

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();
}

const abcd = () => {


console.log("hello1")
}

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’);

2 Add Data to file:


fs.appendfilesync(“filename.txt”,’content of file’);
3 Read File Data:
This method read the entire file into buffer.
fs.readFileSync("newfile.txt");
const data= fs.readFileSync("newfile.txt");
console.log(data);
//Includes additional data type called buffer , buffer is used to store
binary data.
//while reading from a file or receiving packets over the network.

4 Instead of buffer data , i want original data inside a file:


actual_data=data.toString();
console.log(actual_data);
Another way to read a file:
fs.readFileSync("newfile.txt",”utf-8”);
UTF-8 stands for “Unicode Transformation Format - 8 bits.”
This means that UTF-8 takes the code point for a given Unicode
character and translates it into a string of binary. It also does the
reverse, reading in binary digits and converting them back to
characters.

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);

• It returns an object that contains operating system specified constants.


Example: error codes, signal constants, priority constant, dlopen constants, etc.
• Signal constants can be accessed by using os.constants.signals
• priority constants can be accessed by using os.constants.priority.
• Error contants can be acessed by using console.log(os.constants.errno);

12. cpus() : Returns an array containing information about the


computer's CPUs
console.log(os.cpus);
Path Module
• The Path module provides a way of working with directories and file
paths.
• The node:path module provides utilities for working with file and
directory paths. It can be accessed using:
const path = require(‘path');
Path Module Properties And Methods
1. path.dirname(‘path’) : The path.dirname() method returns the
directory name of a path, similar to the Unix dirname command.
console.log(path.dirname(‘path’));
2. path.extname(‘path’): Returns the file extension of a path.
console.log(path.extname(‘path’));
3. path.basename(‘path’) : Returns the last part of a path.
console.log(path.basename(‘path’));
4. path.parse(‘path’): Formats a path string into a path object.
console.log(path.parse(‘path’));
• To accesss the property inside the object returned by parse :
console.log(path.parse(‘path’.propertyname));

5. delimiter : Returns the delimiter specified for the platform.


A delimiter is a sequence of one or more characters for specifying the boundary between
separate, independent regions in plain text, mathematical expressions or other data
streams. An example of a delimiter is the comma character, which acts as a field delimiter
in a sequence of comma-separated values.
console.log(path.delimiter);

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);

You might also like