Questionnaire-of-the-Facebo.../surveyController.js

91 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-03-10 19:01:44 +00:00
// required packages
var bodyParser = require('body-parser');
2021-03-11 23:56:57 +00:00
var urlencodedParser = bodyParser.urlencoded({ extended: false });
2021-03-10 19:01:44 +00:00
var fs = require('fs');
// read the data file
2021-03-11 23:56:57 +00:00
function readData(fileName) {
2021-03-10 19:01:44 +00:00
let dataRead = fs.readFileSync('./data/' + fileName + '.json');
let infoRead = JSON.parse(dataRead);
return infoRead;
}
// read the data file
2021-03-11 23:56:57 +00:00
function writeData(info, fileName) {
2021-03-10 19:01:44 +00:00
data = JSON.stringify(info);
fs.writeFileSync('./data/' + fileName + '.json', data);
}
// update the data file, I use "name" to be equal to fruit, or animal or color
// to match with the file names
// I assume we always just add 1 to a single item
2021-03-11 23:56:57 +00:00
function combineCounts(name, value) {
2021-03-10 19:01:44 +00:00
// console.log(value);
info = readData(name);
2021-03-11 23:56:57 +00:00
// will be useful for text entry, since the item typed in might not be in the list
2021-03-10 19:01:44 +00:00
var found = 0;
2021-03-11 23:56:57 +00:00
for (var i = 0; i < info.length; i++) {
if (info[i][name] === value) {
2021-03-10 19:01:44 +00:00
info[i].count = parseInt(info[i].count) + 1;
found = 1;
}
}
2021-03-11 23:56:57 +00:00
if (found === 0) {
info.push({
[name]: value,
count: 1
});
2021-03-10 19:01:44 +00:00
}
writeData(info, name);
}
// This is the controler per se, with the get/post
2021-03-11 23:56:57 +00:00
module.exports = function(app) {
2021-03-10 19:01:44 +00:00
// when a user goes to localhost:3000/analysis
// serve a template (ejs file) which will include the data from the data files
2021-03-11 23:56:57 +00:00
app.get('/analysis', function(req, res) {
var firstName = readData("firstName");
var lastName = readData("lastName");
var email = readData("email");
var question1 = readData("question1");
var question2 = readData("question2");
var question3 = readData("question3");
var question4 = readData("question4");
var question5 = readData("question5");
var question6 = readData("question6");
var comments = readData("comments");
res.render('showResults', { results: [firstName, lastName, email, question1, question2, question3, question4, question5, question6, comments] });
console.log([firstName, lastName, email, question1, question2, question3, question4, question5, question6, comments]);
2021-03-10 19:01:44 +00:00
});
// when a user goes to localhost:3000/niceSurvey
// serve a static html (the survey itself to fill in)
2021-03-11 23:56:57 +00:00
app.get('/survey', function(req, res) {
res.sendFile(__dirname + '/views/index.html');
2021-03-10 19:01:44 +00:00
});
// when a user types SUBMIT in localhost:3000/niceSurvey
// the action.js code will POST, and what is sent in the POST
// will be recuperated here, parsed and used to update the data files
2021-03-11 23:56:57 +00:00
app.post('/survey', urlencodedParser, function(req, res) {
2021-03-10 19:01:44 +00:00
console.log(req.body);
var json = req.body;
2021-03-11 23:56:57 +00:00
for (var key in json) {
2021-03-10 19:01:44 +00:00
console.log(key + ": " + json[key]);
// in the case of checkboxes, the user might check more than one
2021-03-11 23:56:57 +00:00
if ((key === "question4") && (json[key].length === 4)) {
for (var item in json[key]) {
2021-03-10 19:01:44 +00:00
combineCounts(key, json[key][item]);
}
2021-03-11 23:56:57 +00:00
} else {
2021-03-10 19:01:44 +00:00
combineCounts(key, json[key]);
}
}
// mystery line... (if I take it out, the SUBMIT button does change)
// if anyone can figure this out, let me know!
2021-03-11 23:56:57 +00:00
res.sendFile(__dirname + "/views/index.html");
2021-03-10 19:01:44 +00:00
});
2021-03-11 23:56:57 +00:00
2021-03-10 19:01:44 +00:00
};