Unit 4 FSD 2 QB Solution
Unit 4 FSD 2 QB Solution
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.
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
<html>
<body>
<form action ="/check" method="post">
Username: <input type="text" name="uname"/>
</br></br>
password: <input type="text" name="pwd"/>
<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
"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);
QB-121 , 138
<html>
<head>
<title> Website</title>
</head>
<body>
<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"
}
));
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)=>
{
})
app.listen(8004,()=>
{
console.log("server running at 8001");
});
QB-122
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
{ res.set ("content-type","text/html");
res.send ("<h1>Hello</h1>");
});
});
});
});
});
app.listen (5000,()=>
})
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>
<head>
<link href="index.css" rel="stylesheet"/>
</head>
<body>
<h1> welcome </h1>
<h2> Hello world</h2>
</body>
</html>
App.js
const expr = require("express");
{ 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
<html>
<body>
<form action="/process_post" method="post">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
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,()=>
{
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>
</body>
</html>