Week 3
Week 3
Week 3
JS
- Operating System (os) and File System (fs) packages
In Node.js, the os and fs core modules are essential for interacting with the operating system
and the file system, respectively.
Operating System (os) Module
The os module provides utilities to interact with the operating system, useful for getting system-
specific information.
Key Methods:
os.arch(): Returns the CPU architecture (e.g., 'x64').
os.platform(): Returns the OS platform (e.g., 'win32', 'darwin', 'linux').
os.cpus(): Provides details about each CPU core, such as model, speed, and times.
os.freemem(): Returns available system memory in bytes.
os.totalmem(): Gives the total memory in bytes.
os.hostname(): Returns the system’s hostname.
os.uptime(): Provides system uptime in seconds.
const os = require('os');
console.log(`Platform: ${os.platform()}`);
console.log(`CPU Architecture: ${os.arch()}`);
console.log(`Free Memory: ${os.freemem()} bytes`);
/////////////Coding index.js////////////////////
const fs = require('fs');
// Writing to a file
fs.writeFile('example.txt', 'Hello, Node.js!', (err) => {
if (err) throw err;
console.log('File created and written to.');
const os = require('os');
console.log('Platform:' + os.platform());
console.log('CPU Architecture:' + os.arch());
console.log('Free Memory:' + os.freemem() + 'bytes');
const fs = require('fs');
// Writing to a file
fs.writeFile('example.txt', 'Hello, Node.js!', (err) => {
if (err) throw err;
console.log('File created and written to.');
Task / Practice:
function subtract(a, b) {
return a - b;
}
// Export the functions so we can use them in another file
module.exports = {
add,
subtract,
};
ES6 Module
In ES6, you can use export and import functionality (variable, functions, objects, classes etc.)
instead of module.exports and require to organize your code. ES6 modules provide a more
straightforward syntax and are widely used in modern JavaScript environments.
Step 1: Create a Module with Addition and Subtraction Functions
We’ll make a file called math.js with two functions for addition and subtraction, and export
them using the ES6 syntax.
File: math.js
// Define addition and subtraction functions
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
Lodash package
Lodash provides many functions that simplify common programming tasks.
Lodash is a popular JavaScript utility library that helps you write and maintain your code. This
library uses concepts from functional programming to make data manipulation, object handling,
array operations, and many other tasks easier.
Key Features:
Array Functions: Manipulate arrays easily (e.g., map, filter, reduce).
Object Functions: Work with objects (e.g., get, set, cloneDeep).
Commonly Used Functions:
_.map(): Transform each element in an array.
_.filter(): Get elements that meet a condition.
_.reduce(): Combine array values into a single value.
_.get(): Access a value in an object.
_.set(): Set a value in an object.
_.cloneDeep(): Create a deep copy of an object.
_.debounce(): Delay function execution.
_.throttle(): Limit how often a function runs.
Installation:
npm install lodash
Example Usage:
const _ = require('lodash');
Data Types:
Strings: Enclosed in double quotes (e.g., "Alice").
Numbers: No quotes (e.g., 30).
Booleans: true or false.
Null: Represents empty values.
// API WEEK 3
const _= require('lodash') //lodash require
app.use(express.json()) // Informs Node.js that incoming data will be in JSON format (key-value
pairs as an object)
let obj= {} //object created
app.post('/post-data', (req, res)=>{ //rout and call back function, assum login form
_.set(obj, 'item', req.body.itemName) //.set is loadash function used store data in any
object, it has 3 parameters
//1. obj. The data coming from the frontend etc. will be stored in this variable.
//2. Since we are sending data in JSON format, we need to provide a key here, and the data
coming from the frontend will be the value for this key.
// It means that when the user sends the name of the item, this name comes in req.body
under itemName
res.send(obj)
})
//http://localhost:3000/post-data