Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished assignment #29

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
124 changes: 124 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const express = require('express')
const cookieParser = require("cookie-parser");
const exphbs = require("express-handlebars");
const fs = require('fs');
const session = require("express-session");
const bodyParser = require('body-parser');



const app = express()
app.use(express.static(`${__dirname}/public`))
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: true }))



app.use(
session({
secret: "123456",
resave: false,
saveUninitialized: true
})
);


app.use(cookieParser());


app.get('/', (req, res) => {
if (req.session.visited) {

let params = {};
if (req.cookies["good-evil"]) {
if (req.cookies["good-evil"] == "good") {
params["good"] = "good"
} else {
params["evil"] = "evil";
}
}
params.favColor = req.cookies.favColor;
params.favFood = req.cookies.favFood;

let resumes = {
"1": "Accountant",
"2": "Teacher",
"3": "Whatever it is Alexandra does",
"4": "Unicorn Breeder",
"5": "Web Developer"
}
params.resume = [];

params.resume.push(resumes[req.cookies.insanity]);
if (params["good"]) {
params.resume.push("Rescued baby seals");
}

if (params["evil"]) {
params.resume.push("Tricked people into stepping on Legos");
}

let likes = ["puppies", "rainbows", "unicorns"];
if (req.cookies.favColor) {
likes.push(req.cookies.favColor);
}
if (req.cookies.favFood) {
likes.push(req.cookies.favFood);
}
let dislikes = ["TATTOOS (hint hint)", "Brakes", "Spiders"];
if (params["evil"]) {
dislikes.push("Helping People")
}
if (params["good"]) {
dislikes.push("Tabs")
}

if (Number(req.cookies.insanity) > 3) {
likes.push("Repetition");
}

insanityLevel = Number(req.cookies.insanity);
for (let i = insanityLevel - 1; i > 0; i--) {
likes.push("Repetition");
};

params["likes"] = likes;
params["dislikes"] = dislikes;


let biography = {
1: "Grew up, got married, had kids, died",
2: "Grew up, went to India, went to Africa, got married at 50.",
3: "Grew up, went to Vegas, blew all his money on blackjack and died penniless",
4: "Never grew up, became a Lost Boy, fought Captain Hook to the death",
5: "Spent $2000 on an online bootcamp run by people he never met in his life and spent 10 hours a day coding a room talking to people who aren't there"
}

params["biography"] = biography[insanityLevel];


console.log(params)
res.render('main', params)
} else {
req.session.visited = true;
res.render('main')
}
});

app.post('/', (req, res) => {
const favColor = req.body.favColor || "";
const insanity = req.body.insanity || "";
const favFood = req.body.favFood || "";
const goodEvil = req.body["good-evil"] || "";

res.cookie('favColor', favColor);
res.cookie('favFood', favFood);
res.cookie("good-evil", goodEvil);
res.cookie("insanity", insanity);

res.redirect("back");
});


app.listen(3000, () => console.log('Example app listening on port 3000!'))
Loading