FSD 7
FSD 7
FSD 7
Practical 4
Aim: Create a simple web server using the HTTP module in Node.js.
Code:
Filename:pr_4.js
var http = require('http');
http.createServer(function(req,res){
res.write("Full Stack Development!!")
res.end()
}).listen(8080);
Output:
Practical 5(a)
Aim: a. Create a functional component to perform increment and
decrement operation onclick event by using useState in Reactjs.
Code:
Filename: App.js
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<P/>
</div>
);
}
Filename: pra5.js
import React,{useState} from 'react'
const inceremet=()=>{
setNum(num+1);
}
const decrement=()=>{
if(num>0)
{
setNum(num-1);
}
else{
setNum(0);
}
}
return (
<div>
<div className="card-design">
<h1 className="text-design">{num}</h1>
<button className="button-design" onClick={inceremet}>Increment</button>
<button className="button-design" onClick={decrement} >Decrement</button>
</div>
</div>
)
}
Run command: cd pra5.js
npm start
Output:
Practical 5(b)
Aim: Create a class component to update state onclick event in Reactjs.
Code:
Filename: App.js
import logo from './logo.svg';
import './App.css';
import P from "./Pracb.js";
function App() {
return (
<div className="App">
<P/>
</div>
);
}
Filename: Pracb.js
import React, { Component } from 'react'
render() {
return (
<div>
Output:
Practical 6
Aim: Write a Node.js program to demonstrate the use of various
conditional statements.
Code:
Filename: pr6
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if (isNaN(num)) {
console.log('Invalid input. Please enter a number.');
} else if (num > 0) {
console.log('The number is positive.');
} else if (num < 0) {
console.log('The number is negative.');
} else {
console.log('The number is zero.');
}
rl.question('Do you want to check another number? (y/n): ', (answer) => {
if (answer.toLowerCase() === 'y') {
rl.close();
console.log('\nRestarting...\n');
require('./conditionalStatements.js');
} else {
console.log('Exiting...');
rl.close();
}
});
});
Run command: node pr_6.js
Output:
Practical 7
Aim: Write a Node.js program to demonstrate use of path,fs, os and util
module.
Path Module:
var a = require("path"); console.log(a.basename('F:\IOS-196.docx'));
console.log(a.extname('F:\IOS-196.docx')); console.log(a.format({
dir: 'C:\\path\\dir',
base: 'file.txt', }));
console.log(a.isAbsolute('.'));
console.log(a.normalize('/foo/bar//baz/asdf/quux/..'));
console.log(a.parse('/home/user/dir/file.txt'));
console.log(process.env.a);
console.log(a.dirname('/foo/bar/baz/asdf/quux'));
console.log(a.join('/foo', 'bar', 'baz/asdf', 'quux', '..'));
Output:
Fs Module:
const fs = require("fs");
const { format } = require("path");
fs.writeFileSync("t1.txt", "krupa");
var read = fs.readFileSync("t1.txt");
console.log(read.toString());
fs.appendFileSync("t1.txt", "full stack development");
try { fs.unlinkSync("t1.txt");
console.log("File deleted");}
catch (err) {
console.log("Error:", err);}
fs.unlink("t1.txt",
function (err) { if (err) { console.log("error:", err);
}
else { console.log("file deleted");}});
Output:
Os Module:
console.log("Hello World");
const a=require('node:os');
console.log(a.arch());
console.log(a.cpus());
console.log(a.freemem());
console.log(a.homedir());
console.log(a.hostname());
console.log(a.networkInterfaces());
console.log(a.release());
console.log(a.userinfo());
console.log(a.impdir());
console.log(a.totalmem());
console.log(a.type());
Output:
Util Module:
var util=require('util');
console.log(util.isArray([]));
console.log(util.isArray({a:10}));
console.log(util.isDate(new Date()));
console.log(util.isDate({}));
console.log(util.isError(new Error('This is an Error.')));
console.log(util.isError({message: 'FSD'}));
Output: