[go: up one dir, main page]

0% found this document useful (0 votes)
15 views18 pages

Node Js 3

The document discusses the FS and OS modules in Node.js, highlighting synchronous vs asynchronous file reading methods. It explains the OS module's utility functions for retrieving system information and how to return JSON data in HTTP responses using Node.js. Additionally, it mentions the use of JSON.stringify for formatting JavaScript values as JSON strings.

Uploaded by

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

Node Js 3

The document discusses the FS and OS modules in Node.js, highlighting synchronous vs asynchronous file reading methods. It explains the OS module's utility functions for retrieving system information and how to return JSON data in HTTP responses using Node.js. Additionally, it mentions the use of JSON.stringify for formatting JavaScript values as JSON strings.

Uploaded by

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

FS MODULE, OS MODULE AND RESPONSE

HANDLING
FS – SYNCHRONOUS AND ASYNCHRONOUS
READING
When you use synchronous methods in Node.js, such as fs.readFileSync(),
the code execution is blocked until the operation is complete. This means
no other code can run during the file reading process. While this is simpler
to reason about, it can be detrimental to performance when used on a
server handling multiple I/O operations concurrently.
THE BUILT-IN FS (FILE SYSTEM) MODULE IN NODE.JSPROVIDES THE
READFILE() METHOD, WHICH READS FILES ASYNCHRONOUSLY. THIS
MEANS THAT WHILE NODE.JS IS WAITING FOR THE FILE READ OPERATION
TO COMPLETE, IT CAN CONTINUE PROCESSING OTHER TASKS.
OS MODULE
The OS module in Node.js provides operating
system-related utility methods and properties. It
helps retrieve system information such as CPU
details, memory usage, and network interfaces,
enabling you to write system-aware applications.
FUNCTIONS()
•os.arch(): Returns the CPU architecture of the operating system
(e.g., ‘x64’, ‘arm’).

•os.cpus(): Provides an array of objects describing each CPU/core


installed.

•os.freemem(): Returns the amount of free system memory in bytes.

•os.homedir(): Returns the path to the current user’s home directory.

•os.hostname(): Returns the hostname of the operating system.


•os.networkInterfaces(): Returns a list of network interfaces and
their details.

•os.platform(): Returns the operating system platform (e.g.,


‘linux’, ‘darwin’).

•os.release(): Returns the operating system release.

•os.totalmem(): Returns the total amount of system memory in


bytes.

•os.uptime(): Returns the system uptime in seconds.


RETURN JSON USING NODE.JS
JSON stands for JavaScript Object Notation. It is one of the most
widely used formats for exchanging information across applications.
In order to return JSON data the res.setHeader method is used to
set the Content-Type header field of the response to the value
application/json.
The JSON data object is then returned by using the res.end
function. In order to output it as an formatted string the
JSON.stringify method is used.
JSON.stringify() is a method in JavaScript that converts a
JavaScript value to a JSON string.
EXAMPLE 1
EXAMPLE-2
OUTPUT
NODE HTTP SEND HTML
HTTP server with three HTML files in the docs subdirectory.

File-01
FILE -02 (CONTACT.HTML) FILE -03 (INDEX.HTML)

You might also like