FSD 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Enrollment No:202103103510305

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:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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

import P from "./prac5.js";

function App() {
return (
<div className="App">
<P/>
</div>
);
}

export default App;

 Filename: pra5.js
import React,{useState} from 'react'

export default function Changevalue_n() {


const [num,setNum]=useState(0);

const inceremet=()=>{
setNum(num+1);
}
const decrement=()=>{
if(num>0)

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

{
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:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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

export default App;

 Filename: Pracb.js
import React, { Component } from 'react'

export class Changevalue extends Component {


constructor(props){
super(props);
this.state={
name:"krupa"
}
this.change_name = this.change_name.bind(this)
}
change_name() {
this.setState({name:"ira"});

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

render() {
return (
<div>

<button onClick={this.change_name}>Change name</button>


<h1>{this.state.name}</h1>
</div>
)
}
}

export default Changevalue;


run command: cd pracb
npm start

Output:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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

rl.question('Enter a number: ', (number) => {


const num = parseInt(number);

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 {

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

console.log('Exiting...');
rl.close();
}
});
});
Run command: node pr_6.js

Output:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

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:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

 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'}));

CGPIT/IT/SEM-6/Full Stack Development(IT5024)


Enrollment No:202103103510305

Output:

CGPIT/IT/SEM-6/Full Stack Development(IT5024)

You might also like