0% found this document useful (0 votes)
78 views10 pages

Unit 4 FSD 2 QB Solution

The document describes an Express.js script that: 1. Defines a JSON array of student objects with name and age properties 2. Renders the student array on the '/' route 3. Sorts the student array by age in ascending order and renders the sorted names on the '/sortednames' route 4. Defines the app to listen on port 5200 It provides an Express.js solution to define a JSON array of objects, sort the array by a property, and render the sorted values on a specific route.

Uploaded by

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

Unit 4 FSD 2 QB Solution

The document describes an Express.js script that: 1. Defines a JSON array of student objects with name and age properties 2. Renders the student array on the '/' route 3. Sorts the student array by age in ascending order and renders the sorted names on the '/sortednames' route 4. Defines the app to listen on port 5200 It provides an Express.js solution to define a JSON array of objects, sort the array by a property, and render the sorted values on a specific route.

Uploaded by

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

Unit-4 QB solution FSD-2

QB-118

Write an express.js script to define one JSON array of 3 objects having members name and age.
Names must be sorted according to age. If user requests “sorted Names” URL, then all names
should be printed according to ascending order of age.

const expr = require("express")


const app = expr();
student = [{name:"abc",age:28},
{name:"def",age:40},
{name:"xyz",age:10}]
app.get ("/",(req,res)=>{
res.set ("content-type","text/html")
res.send (student)
})
app.get ("/sortednames ",(req,res)=>{
res.set ("content-type","text/html")
for (i=0;i<student.length;i++){
for (j=0;j<student.length;j++){
if (student[i].age>student[j].age){
temp = student[i];
student [i] = student[j];
student [j] = temp
}
}
}
for (k=0;k<student.length;k++){
res.write ("<center><h2>"+student[k].name+ " = " +student[k].age +"</h2></center>")
}
res.send ()
})
app.listen (5200)

QB-119

Process a form using post method. Form has fields like username, password, confirm password,
gender, submit and reset buttons. After entering all fields, If password and confirm password
matches, then form should be processed and all relevant and selected fields’ values should be
printed. Otherwise, by printing warning message in red color, it should terminate. No need to
write file having form elements

index.html(No need to write this file in answer)

<html>
<body>
<form action ="/check" method="post">
Username: <input type="text" name="uname"/>
</br></br>
password: <input type="text" name="pwd"/>

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

<br></br>
confirm password:<input type="text" name="conf_pwd"/><br>
Gender:
<input type="radio" name="a1" value="Male">Male
<input type="radio" name="a1" value="Female">Female<br>
<input type="submit"/><br>
<input type="reset"/>

</form>
</body>
</html>
App.js

const expr=require("express");
const app=expr();
const bp=require("body-parser");
const url=bp.urlencoded(
{
extended:false
});
const staticPath=__dirname;
app.use(expr.static(staticPath));
app.post("/check",url,(req,res)=>
{ res.set("content-type","text/html")
if(req.body.pwd==req.body.conf_pwd)
{

res.write(`<h1> Username:${req.body.uname}
password:${req.body.pwd} confirm password:${req.body.conf_pwd}</h1>`);
gender=req.body.a1;
res.write(`<h1> gender=${gender}</h1>`);
}
else
{res.write(`<h1 style="color:red"> password and confirm password are not
same</h1>`);}
res.send()
})
app.listen(8000,()=>
{
console.log("server running at 8000");
});

QB-120

write an express.js script to load an HTML file having username and password and submit button.
On clicking submit button. It should jump on "check" page using "POST" method. If username is

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

"admin" , then jump on next middleware to print "welcome… admin" , if username is not
"admin" , then stay on same middleware to print "warning msg" in red color.

Index.html

<html>
<body>
<form action ="/check" method="post">
Username: <input type="text" name="uname"/>
</br></br>
password: <input type="text" name="pwd"/>
<br></br>
<input type="submit"/>
</form>
</body>
</html>

App.js

const expr=require("express");
const app=expr();
const bp=require("body-parser");
const url=bp.urlencoded(
{
extended:false
});
console.log(url);
const staticPath=__dirname;
app.use(expr.static(staticPath));
app.post("/check",url,(req,res,next)=>
{ if(req.body.uname=="admin")
{
next();

else{
res.send("<h1 style='color:red'>wrong credentials</h1>")
}
});
app.post("/check",url,(req,res,next)=>
{
res.write(`<h1>welcome...${ req.body.uname}</h1>`)
res.send();
}
).listen(3001);

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

QB-121 , 138

write a sript to meet following requirements.


1) create index.html page and open it on localhost
2) after clicking submit button, it should jump to savesessionpage. store username in session.
3) After saving session, redirect to fetchsession page and read session value. put a logout link
button here.
4) destroy the session on this page and redirect to index.html
index.html

<html>
<head>
<title> Website</title>
</head>
<body>

<form action="/savesession" method="get">


<fieldset>
<legend>Sign-up</legend>
Username: <input type="text" name="uname"/>
</br>
password: <input type="text" name="pwd"/>
<br>

<input type="submit"/>
</fieldset>
</form>
</body>
</html>

App.js

const expr=require("express");
const app=expr();
const sess=require("express-session");
app.use(expr.static(__dirname,{index:"sessiontask.html"}))
app.use(sess(
{
resave:true,
saveUninitialized:true,
secret:"Heloo"
}
));

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

app.get("/savesession",(req,res)=>
{
if(!req.session.u)
{ req.session.u=req.query.uname
res.redirect("/fetchsession")
}
else
{
console.log("Session is already set...");
}
})
app.get("/fetchsession",(req,res)=>
{

res.write(`<h1 style="color:blue;border:5px solid black;font-


style:italic"> Welcome... ${req.session.u} <h1>`);
res.write(`<button><a href="deletesession">Logout</a></button>`);
res.send();
})
app.get("/deletesession",(req,res)=>
{
req.session.destroy();
res.redirect("/")

})
app.listen(8004,()=>
{
console.log("server running at 8001");
});
QB-122

write an express.js script to print "hello World" also run on localhost:4000

const expr=require("express");
const app=expr();
app.get(“/”,(req,res)=>
{
res.write(“hello world”);
}
app.listen(4000);

QB-123

write an express.js script to use of routing method using home , about , contact , temp page and
print message. /home- welcome to my home page , /about -welcome to my about page,
/contactwelcome to my contact page ,/temp - welcome to my temp page

index.js

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

const expr = require("express");

const app = expr();

app.get ("/", (req,res)=>

{ res.set ("content-type","text/html");

res.send ("<h1>Hello</h1>");

});

app.get ("/home", (req,res)=>

{ res.send ("Welcome to my home page");

});

app.get ("/about", (req,res)=>

{ res.send ("welcome to my about page”);

});

app.get ("/contact", (req,res)=>

{ res.send ("welcome to my contact page”);

});

app.get ("/temp", (req,res)=>

{ res.send ("welcome to my temp page”);

});

app.listen (5000,()=>

{ console.log ("server started");

})

QB-125
write an express.js script to make one index.html file in this file we write simple heading tag and
make one css file index.css put style using appropriate selector and link with js file and show
output on localhost:3030.

Index.css
h1
{ color:”red”}

h2
{color:”green”}

Index.html
<html>

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

<head>
<link href="index.css" rel="stylesheet"/>

</head>
<body>
<h1> welcome </h1>
<h2> Hello world</h2>
</body>
</html>

App.js
const expr = require("express");

const app = expr();

app.get ("/", (req,res)=>

{ res.sendFile("/index.html");

res.send ();

}).listen(3030);

QB-126
write an express.js to implement a form that allows users to submit data via POST request. Write
code to create a route that handles the POST request and logs the submitted data to the
console.
const expr=require("express");
const app=expr();
const bp=require("body-parser");
const url=bp.urlencoded({extended:true});
const staticpath=__dirname;
app.use(expr.static(staticpath));
app.post("/process_post",url,(req,res)=>
{ firstname=req.body.firstname
Lastname= req.body.lastname
gender=req.body.a1
console.log(firstname);
console.log(lastname);
console.log(gender);
res.send();

}
);
app.listen(5000,()=>
{
console.log("server start");
});

index.html

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

<html>
<body>
<form action="/process_post" method="post">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>

<input type="radio" name="a1" value="male"/>MAle


<input type="radio" name="a1" value="female"/>FEMAle
<input type="submit"/>
</form>
</body>
</html>

QB-127
write an express.js to implement a form that allows users to submit data via POST request. Write
code to create a route that handles the POST request and saves the submitted data to a json
format.

Index.html
<html>
<body>
<form action ="/check" method="post">
Username: <input type="text" name="uname"/>
</br></br>
password: <input type="text" name="pwd"/>
<br></br>
<input type="submit"/>
</form>
</body>
</html>

App.js

const expr=require("express");
const app=expr();
const bp=require("body-parser");
const url=bp.urlencoded(
{
extended:false
});
const staticPath=__dirname;
app.use(expr.static(staticPath));
app.post("/check",url,(req,res)=>
{
const data={uname:req.body.uname,password:req.body.pwd}
res.send(data);
})
app.listen(8000,()=>

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

{
console.log("server running at 8000");
});

QB-136
Write an ExpressJS to take a textarea & submit button. After clicking submit button the content of
textarea should be represented on next page by writing each sentence (separated by dot) in new
line.
src/app.js

const expr=require("express");
const path=require("path");
const app=expr();
const staticpath=path.join(__dirname);
console.log(staticpath);
app.use(expr.static(staticpath,{index:"third.html"}));
app.get("/process_get",(req,res)=>
{ res.set("content-type","text/html");
data=req.query.textarea1
console.log(data);
a=data.split('.');
for(x in a)
{res.write(a[x]+"<br>");
}
res.end();
});
app.listen(2501,()=>
{
console.log("server start");
});

src/third.html

<html>
<head>

</head>
<body>
<form action="/process_get" method="get">
<textarea row="5" cols="15" name="textarea1"> this is an textarea
tag. we are doing express js.this is demo</textarea>
<input type="submit"/>
</form>

Prepared By: Hiral Patel


Unit-4 QB solution FSD-2

</body>
</html>

Prepared By: Hiral Patel

You might also like