const express=require('express') const app=express() const jsdom=require("jsdom") // app.use(express.json()); // app.use(express.urlencoded()); const { body, validationResult } =...

1 answer below »
need proper comments on each step till send and explain its working


const express=require('express') const app=express() const jsdom=require("jsdom") // app.use(express.json()); // app.use(express.urlencoded()); const { body, validationResult } = require("express-validator"); const bodyParser=require('body-parser') app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) //server run on port 8078 app.listen(8078, function () { console.log("server is running port -- 8078"); }); //validate user app.post( "/users", body("name").isLength({ min: 1, max:20 //validate user length }), body("password") .isLength({ min : 8 , max : 20 //validate user password }),body("comment").isLength({ max : 100, //validate user comment length }), (req, res) => { const errors = validationResult(req);//passing request to the validation function if (!errors.isEmpty()) { res.status(200).send(` You have entered wrong querry Please fill the form again. `); } //If there is any error than the error message will reflect //checking for the checkbox let c1=0,c2=0,c3=0; let flag = 1; if(req.body.optradio=='1'){ c1++; }else if(req.body.optradio=='2'){ c2++; } else if(req.body.optradio=='3'){ c3++; } else{ flag=0; } if (flag == 0) { return res .status(400) .send(` You have entered wrong querry Please fill the form again. `);//If radio button is not checked than an error will shown. } letdata = req.body; letsql = `INSERT INTO user set?`; // data.createdAt = 15 / 12 / 2000; // data.updatedAt = 15 / 12 / 2000; // con.query(sql, data, (err, result) => { // if (!err) { res.status(200).send(` Thank you for submitting your query Username entered:${req.body.name} Password entered:${req.body.password} Comment entered:${req.body.comment} Option entered:${req.body.optradio} We will respond to your query in the 24 hours. `); console.log('Done3') // else console.log(err); res.status(200).json({ success:true, message:'Login Successful' }) } ); //USER VALIDATOION FORM app.get("/index", (req, res) => { res.send(` Express Server Example Simple Form Please complete the form: Name: Password: Comment:Your comment here... Prefered contact: Email Mobile Post Submit `); }); 9.3D-Server side Validation of Data 9.3D: Server-side Validation of Data Tasks Validation of data entered by a user (or other source) can be done on the Client- side using Javascript routines, as we did in Task 7.3C. It is good programming practice for a server not to rely solely on the client-side and it should also perform validation before processing/storing/using the submitted data. Your tasks here is to implement server-side validation of data entered from the user. You can use the simple form from Task 9.1P as the source of the data: Task9.1.1 Example form layout In validating this form, the server should check the following fields are valid: 1. The name field is not empty (i.e., it contains text) 2. The password fields is at least 8 characters and no more than 20 characters in size. SIT774 Web Technologies and Development Task 9.3D 2021/T1 1/2 3. The comment field is less than 100 characters in size. 4. That one of the optradio fields has been selected. The server should return one of two responses: 1. Successfully validated form - Normal ‘Thank you for submitting your query’ response, as with original 9.1P task. 2. Invalid form - A response stating which of the three fields were not valid. What will you submit? You should submit: Source code of the Node.js file (i.e., the .js file). Screenshot of the browser window showing the form web page with VALID entered data. Screenshot of the browser window showing the response page given a VALID form is submitted. Screenshot of the browser window showing the form web page with INVALID entered data. Screenshot of the browser window showing the response page given an INVALID form is submitted. SIT774 Web Technologies and Development Task 9.3D 2021/T1 2/2 9.3D: Server-side Validation of Data Tasks What will you submit? const express=require('express') const app=express() const jsdom=require("jsdom") // app.use(express.json()); // app.use(express.urlencoded()); const { body, validationResult } = require("express-validator"); const bodyParser=require('body-parser') app.use(bodyParser.urlencoded({extended:false})) app.use(bodyParser.json()) //server run on port 8078 app.listen(8078, function () { console.log("server is running port -- 8078"); }); //validate user app.post( "/users", body("name").isLength({ min: 1, max:20 //validate user length }), body("password") .isLength({ min : 8 , max : 20 //validate user password }),body("comment").isLength({ max : 100, //validate user comment length }), (req, res) => { const errors = validationResult(req);//passing request to the validation function if (!errors.isEmpty()) { res.status(200).send(` You have entered wrong querry Please fill the form again. `); } //If there is any error than the error message will reflect //checking for the checkbox let c1=0,c2=0,c3=0; let flag = 1; if(req.body.optradio=='1'){ c1++; }else if(req.body.optradio=='2'){ c2++; } else if(req.body.optradio=='3'){ c3++; } else{ flag=0; } if (flag == 0) { return res .status(400) .send(` You have entered wrong querry Please fill the form again. `);//If radio button is not checked than an error will shown. } letdata = req.body; letsql = `INSERT INTO user set?`; // data.createdAt = 15 / 12 / 2000; // data.updatedAt = 15 / 12 / 2000; // con.query(sql, data, (err, result) => { // if (!err) { res.status(200).send(` Thank you for submitting your query Username entered:${req.body.name} Password entered:${req.body.password} Comment entered:${req.body.comment} Option entered:${req.body.optradio} We will respond to your query in the 24 hours. `); console.log('Done3') // else console.log(err); res.status(200).json({ success:true, message:'Login Successful' }) } ); //USER VALIDATOION FORM app.get("/index", (req, res) => { res.send(` Express Server Example Simple Form Please complete the form: Name: Password: Comment:Your comment here... Prefered contact: Email Mobile Post Submit `); });
Answered Same DayJun 08, 2021SIT774Deakin University

Answer To: const express=require('express') const app=express() const jsdom=require("jsdom") //...

Ali Asgar answered on Jun 08 2021
142 Votes
const express=require('express')
const app=express()
const jsdom=require("jsdom")
// app.use(express.json());
// app.use(express.urle
ncoded());
const { body, validationResult } = require("express-validator");
const bodyParser=require('body-parser')
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
/********************************************************************
The server is listening on port 8078 for any client request
The server is waiting for client to send data.
********************************************************************/
app.listen(8078, function () {
//while waiting write in log that server is running on port 8078
console.log("server is running port -- 8078");
});
/********************************************************************
Validation code for the data entered in the form.
1. Check for 'name' field for not empty
the isLength validator checks for minimum and maximum    length of the fields.
********************************************************************/
app.post(
"/users",
body("name").isLength({
min: 1,
max:20
//Check if the minimum length of name field is 1 (that is NOT empty)
}),
/********************************************************************
2. Check for 'password' field to be min 8 characters and max 20 characters
isLength validator is used...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here