0% found this document useful (0 votes)
19 views63 pages

Node - Js-Part1 U

The document provides a comprehensive guide on using Node.js, including installation of necessary extensions in Visual Studio Code, creating and running JavaScript files, and utilizing built-in modules like os and path for system-related operations. It covers file reading and writing operations using fs module, handling errors, and asynchronous programming concepts. Additionally, it explains how to manage files and directories, including creating, renaming, and appending to files.

Uploaded by

bhowmikpinki59
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)
19 views63 pages

Node - Js-Part1 U

The document provides a comprehensive guide on using Node.js, including installation of necessary extensions in Visual Studio Code, creating and running JavaScript files, and utilizing built-in modules like os and path for system-related operations. It covers file reading and writing operations using fs module, handling errors, and asynchronous programming concepts. Additionally, it explains how to manage files and directories, including creating, renaming, and appending to files.

Uploaded by

bhowmikpinki59
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/ 63

Node.

Js
Local and Export Modules
Node.js File system -
To check wether Node.js installed in
your system or not.
In Visual studio we have already installed an Extension , Still need to install few
more extensions
Install Prettier code formatter
After installing prettier code
Install Vscode -icons
After Installing VS code Icons
Live server installation
After Installing Live server
Install Code runner
Now go back to explorer
Click file open folder
Create a New folder
Created a new folder

Select the folder


Create a new java script
file …
Write a simple javascript code
Click Terminal and open
New Terminal
It shows complete path of the folder

When you give ls command it will


show the files in it .
Just by giving node keyword and js file name (server )
You will get the output
"In Node.js, console output that would typically be printed in the browser's console (when using JavaScript in the
browser) is instead printed in the terminal."

• If you're working with frontend (React, Vanilla JS, etc.) → Browser Console is best

• If you're working with backend (Node.js, Express, Databases) → Terminal Console is the best
Just by giving node
command you will
enter into the node
environment .

You can do basic


operations like python
Press Ctrl + e
to exit
Use the built-in os module in Node.js to retrieve system-
related information.

\\Import the os module


Execute the J.S file

You can see all


the os related
information
When you need
system paths, like the
home directory or
temp folder use os
module

It gives the Dir name of


the Server j.s file located

It gives the Dir name of the Server


j.s file along with the file name
This line imports the built-in path module in Node.js, which is used for handling and manipulating file and
directory paths.

// Get the directory name


// Get the file name
// Get the file extension

When you need to


construct, modify, or
resolve file paths.use
path module
Create a New File Math.js
We imported the math module to
server.js
We can see the Add function is
executed here
Output when calling all the functions
This is another way of importing the math module and calling the
functions……
This is another way of exporting the function directly.
Stil we will get the same output ……….
In the official website of nodejs we will get the documents
particular to our version
Choose the version you
installed in your system
We can see lot of modules
available.

We already worked with some


basic functions in OS and Path
module.
We can explore a lot about these Modules and functions in it
……………….
We can see lot of
Classes and
Functions in File
system to explore
When I searched for Read I can see lot of functions related to Read operation

From this iam choosing


fs.readFile
We can get some code
snippets for that
Fs.readFile function
Create a folder and give a Name to it
Inside that Folder create a
text file
Enter a text of your choice
Copy this code and paste it in VS
javascript file
err (Error Object) → If an error occurs (e.g., file not found), this will contain the error details.
If no error occurs, err will be null.

This is the code we copied from


the document

data (Buffer/String) → If the file is read successfully, this contains the file's content.

If no encoding is specified, data will be a Buffer; otherwise, it will be a string.


Give the details of the Folder and file from
which we need to read the data .
The readFile function read the content
of the file as buffer by default.
toString() method is used to convert this buffer into a readable string.
We can specify the encoding while reading

Data is already a string, and you don’t need toString()


Uncaught Exceptions

1. process.on('uncaughtException', callback)

This event listener is used to handle uncaught exceptions—errors that


occur synchronously but are not caught by try...catch.

'uncaughtException': This event is emitted when an exception is thrown


and not handled.

callback (err): A function that receives the Error object (err) and handles
it.
…….

2. Error Logging (console.error)

console.error(...) prints the error message to the console.

The corrected template literal (${err}) ensures the actual error


object/message is properly displayed.

3. Graceful Exit (process.exit(1))

process.exit(1); forcibly terminates the application with exit code 1, which


typically indicates an error.
Since there is no exception ,we are getting output as usual
I changed the file name here, there is an exception now.

So the exception is logged now …


In Node.js, asynchronous read means reading a file without blocking the execution of other code.
How Asynchronous Read works in the previous example:

• ReadFile is called to read firstttt.txt.

• Instead of waiting,

• Node.js moves on and logs "Hi".

• Once the file is read, the callback function executes and prints
the file's contents.

• If an error occurs (e.g., file not found), the callback handles it.

• The process.on('uncaughtException', ...) catches any


unhandled errors globally.
path: The Path module, used to handle file paths across different operating systems.

ensures the correct file path on Windows, Linux, and macOS.

____dirname represents the


current directory.

Joins it with 'Files/first.txt'


fs.writeFile (filePath, data, callback)
Write File

Writes 'Hi guys how are you' into book.txt.

If book.txt exists, it overwrites its content.

If book.txt does not exist, it creates a new


file.

(err) => {} If an error occurs, throw err stops execution.


If successful, it logs "Write completed".
Append File Opens book.txt and adds '\n\n Thanks for submitting your data' at the end.

If book.txt does not exist, it creates a new file.

Append should be done with in the call back of write File

Does not overwrite, only adds new content.


We can also use Append to create a new file
Rename a File

This is the new name (magazine.txt) that book.txt will be renamed to.

You might also like