Answer To: 9.2C-Create a Response Page 9.2C: Create a Response Page Tasks You are provided with an online form...
Amit Kumar answered on May 30 2021
app.js
const mysql = require("mysql");
const express = require("express");
var app = express();
var fs = require("fs");
const router = express.Router();
app.use(express.json());
app.use(express.urlencoded());
const { Sequelize, DataTypes, Model } = require("sequelize");
const sequelize = new Sequelize("api", "root", "", {
host: "localhost",
dialect: "mysql",
migrationStorage: "json",
define: {
charset: "utf8",
collate: "utf8_general_ci",
timestamps: true,
},
});
sequelize
.authenticate()
.then((err) => {
console.log("Connection successful");
})
.catch((err) => {
console.log("Unable to connect to database");
});
class Survey extends Model {}
Survey.init(
{
surveyId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
fname: {
type: DataTypes.STRING,
allowNull: true,
},
sname: {
type: DataTypes.STRING,
allowNull: true,
},
gender: {
type: DataTypes.STRING,
allowNull: true,
},
postcode: {
type: DataTypes.STRING,
allowNull: true,
},
bday: {
type: DataTypes.STRING,
allowNull: true,
},
food: {
type: DataTypes.STRING,
allowNull: true,
},
sport: {
type: DataTypes.STRING,
allowNull: true,
},
genre: {
type: DataTypes.STRING,
allowNull: true,
},
interests: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
tableName: "survey",
freezeTableName: true,
timestamps: true,
sequelize,
modelName: "Survey",
}
);
app.use(
router.post("/survey", function (req, res, next) {
const inter = req.body.interests;
const data = req.body;
if (inter.length > 0) {
data.interests = inter.toString();
} else {
data.interests = inter;
}
Survey.create(data)
.then((survey) => {
const date = new Date(survey.createdAt);
var options = {
weekday: "long",
day: "numeric",
year: "numeric",
month: "long",
};
var days = new Date(survey.bday).toLocaleTimeString("en-us", options);
var x = days.split(",");
var y = x[1].split(" ");
const dispbdaydate = x[0] + " " + y[2] + " of " + y[1] + ", " + x[2];
const dispbdaydays =
(new Date() - new Date(survey.bday)) / (1000 * 60 * 60 * 24);
res.send(`
SURVEY REPORT
Survey Report Details
Thank you
'${survey.fname} ${survey.sname}'
for completing and submitting this survey. Please check carefuly
that the details below are correct.
Survery report received on: ${date}.
Firstname entered:
${survey.fname}
Surename entered:
${survey.sname}
Gender entered:
${survey.gender}
Postcode entered:
${survey.postcode}
You were born on:
${survey.bday}
Birtdate entered:
${dispbdaydate}
Age in days:
${dispbdaydays}
Favourite food
${survey.food}
Favourite sports:
${survey.sport}
Favourite Movie Genre:
${survey.genre}
Interests selected:
${survey.interests}
Thank you for your time and our team will be in touch with you
shortly.
`);
})
.catch(next);
})
);
app.use(function (req, res) {
fs.createReadStream(__dirname + "/form6.html").pipe(res);
});
app.listen(8080, function () {
console.log("server is running on port -> http://localhost:8080/");
});
form6.html
ONLINE SURVEY
Personal Info
Your Name:
Your Gender:
Male
Female
Your Postocde:
Your Age:
Interests
Your Favourite Food:
Select a food..
Pizza
Vegatables
Hamburgers
Pasta
Sushi
Kebabs
Curry
Your Favourite Sport:
Select a sport..
AFL
Tennis
Soccer
Cricket
Rugby
Athletics
Swimming
Your Favourite Movie Genre:
Action
Drama
Horror
Comedy
All
Your Interests (multiple):
Reading
Jogging
Knitting
Music
Coding
Fishing
Talking
Dancing
Submit Form
Reset Form
SURVEY REPORT
Survey Report Details
Thank you
'
'
for completing and submitting this survey. Please check carefuly
that the details below are correct.
Survery report received on: .
Firstname entered:
Surename entered:
Gender entered:
Postcode entered:
You were born on:
Birtdate entered:
Age in days:
Favourite food
Favourite sports:
Favourite Movie Genre:
Interests selected:
Thank you for your time and our team will be in touch with you
shortly.
package.json
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"mysql": "^2.18.1",
"mysql2": "^2.2.5",
"sequelize": "^6.6.2"
}
}
package-lock.json
{
"name": "project",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "15.6.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.6.1.tgz",
"integrity": "sha512-7EIraBEyRHEe7CH+Fm1XvgqU6uwZN8Q7jppJGcqjROMT29qhAuuOxYB1uEY5UMYQKEmA5D+5tBnhdaPXSsLONA=="
},
"accepts": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
"integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==",
"requires": {
"mime-types": "~2.1.24",
"negotiator": "0.6.2"
}
},
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
"requires": {
"color-convert": "^1.9.0"
}
},
"any-promise": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
"integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8="
},
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"async": {
"version": "0.9.2",
"resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz",
"integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0="
},
"balanced-match": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"body-parser": {
"version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
"integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
"requires": {
"bytes": "3.1.0",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "~1.1.2",
"http-errors": "1.7.2",
"iconv-lite": "0.4.24",
"on-finished": "~2.3.0",
"qs": "6.7.0",
"raw-body": "2.4.0",
"type-is": "~1.6.17"
}
},
"brace-expansion": {
"version": "1.1.11",
"resolved":...