diff --git a/twoter/app.js b/twoter/app.js index 90626910..c8532292 100644 --- a/twoter/app.js +++ b/twoter/app.js @@ -4,7 +4,11 @@ var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var express = require('express'); -var index = require('./routes/index'); +// clean up the dependencies you're not using! +// also, run `npm init` when you start so you have a `package.json` file -- +// that way, when you run `npm install --save whatever` the dependency info will +// end up in the `package.json`, and people who work on/give you feedback on your app +// will be able to run just `npm install` to get all of your dependencies in the same form you have them. var app = express(); var session = require('express-session') var twote = require('./routes/twote'); @@ -50,7 +54,7 @@ app.use(express.static(__dirname + '/public')); app.use(express.static(path.join(__dirname, 'public'))); -app.use(session({ +app.use(session({ secret: 'superS3CRE7', resave: false, saveUninitialized: false , @@ -59,11 +63,11 @@ app.use(session({ app.get('/', twote.getUsers); app.get('/login', twote.login); -app.get('/newUser', twote.addUser) -app.get('/logout', twote.logout) -app.get('/getLogged', twote.getLogged) -app.get('/addPost', twote.addPost) -app.get('/removePost', twote.removePost) +app.get('/newUser', twote.addUser); // technically this should be a post -- changes the server/database state +app.get('/logout', twote.logout); +app.get('/getLogged', twote.getLogged); +app.get('/addPost', twote.addPost); // technically this should be a post +app.get('/removePost', twote.removePost); // technically this should be a post app.post('/login', twote.postLogin); app.get('/auth/facebook', @@ -76,4 +80,4 @@ app.get('/auth/facebook/callback', -app.listen(3000); \ No newline at end of file +app.listen(3000); diff --git a/twoter/package.json b/twoter/package.json new file mode 100644 index 00000000..6d44ba9c --- /dev/null +++ b/twoter/package.json @@ -0,0 +1,24 @@ +{ + "name": "twoter", + "version": "1.0.0", + "description": "", + "main": "app.js", + "dependencies": { + "body-parser": "^1.15.0", + "cookie-parser": "^1.4.1", + "express": "^4.13.4", + "express-handlebars": "^3.0.0", + "express-session": "^1.13.0", + "mongoose": "^4.4.7", + "mongoose-times": "^0.1.0", + "morgan": "^1.7.0", + "passport": "^0.3.2", + "passport-facebook": "^2.1.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/twoter/public/javascripts/main.js b/twoter/public/javascripts/main.js index 35382d60..f7d4f8db 100644 --- a/twoter/public/javascripts/main.js +++ b/twoter/public/javascripts/main.js @@ -1,5 +1,5 @@ var $form1 = $("#ajax-form"); -var $form2 = $("#ajax-form2"); +var $form2 = $("#ajax-form2"); // more descriptive names, maybe? I have to go cross-reference the index.html file to figure out what these do var $form3 = $("#ajax-form3"); var $form4 = $("#ajax-form4"); var $form5 = $("#ajax-form5"); @@ -34,16 +34,17 @@ var onSuccess3 = function(data, status) { }; var onSuccess4 = function(data, status) { + // Check out https://jsfiddle.net/swalters4925/e8gzd6h9/1/ for the way I like to add structured HTML to a page -- it's an option! $("li[id|='posts']").prepend("
  • post: "+data.author+" posted: "+data.post+"
  • ") console.log(data.post) - + }; var onSuccess5 = function(data, status) { //$("button[value|='"+data.post+"']").prop('disabled', true); $("button[value|='"+data.post+"']").hide(); $("ul[name|='"+data.post+"']").replaceWith(""); - + }; // var onSuccess6 = function(data, status) { @@ -63,7 +64,7 @@ $form1.submit(function(event) { request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); request.send(data); var name = $form1.find("[type='text']").val(); - + console.log(name); $.get("newUser", { name: name @@ -85,7 +86,7 @@ $("input[name|='twote']").on("click", function(){ $form2.submit(function(event) { event.preventDefault(); //var name = $form1.find("[type='submit']").val(); - + //console.log(name); $.get("logout", { name: name @@ -118,13 +119,13 @@ $("button[id|='remove']").on("click",function(event) { //var author = $("h3[id|='user']").val(); var author = $(this).attr("name"); var post = $(this).attr("value"); - console.log("I tried"); + console.log("I tried"); // yes indeed! console.log(author); console.log(post); //var post = $("input[name|='twote']").val(); //console.log("clicked!") //console.log("this"+author); - + $.get("removePost", { author: author, post: post @@ -233,7 +234,7 @@ $("button[id|='remove']").on("click",function(event) { // //$form4.submit(function(event) { // event.preventDefault(); // //var name = $form4.find( "button[type='submit']" ).attr("name"); -// var name = $(this).attr("name"); +// var name = $(this).attr("name"); // console.log(name); // $.get("complete", { // name: name diff --git a/twoter/routes/index.js b/twoter/routes/index.js deleted file mode 100644 index 7cedafbc..00000000 --- a/twoter/routes/index.js +++ /dev/null @@ -1,46 +0,0 @@ -var index = {}; -//var catApp = require("./catApp") -//var db = require('../fakeDatabase'); - -index.home = function(req, res){ - //catApp.getCats() - res.render("home", {"links": [ - {link: "/"}, - {link: "/show"}, - {link: "/add"}, - {link: "/remove"}, - {link: "/sorty"}, - {link: "/sortb"}, - {link: "/sortr"}, - {link: "/sortg"}, - {link: "/sortbb"}] -}); -}; -//var getAll = function(req, res, next){ - - -// index.home = function(req, res){ -// var cats = db.getAll(); -// var msg = "Cat names are: "; -// cats.forEach(function(liz){ -// msg = msg + liz.name + ","; -// }) -// res.render("home", msg); -// }; - - - -// index.add = function(req, res){ -// res.render("add", {"classes": [ -// {name: "bark"}] -// }); -// }; - -// index.remove = function(req, res){ -// res.render("remove", {"classes": [ -// {name: "bark"}] -// }); -// }; - -module.exports = index; - diff --git a/twoter/routes/order.js b/twoter/routes/order.js deleted file mode 100644 index 64cbde71..00000000 --- a/twoter/routes/order.js +++ /dev/null @@ -1,47 +0,0 @@ -var express = require('express'); -var router = express.Router(); -var ord = require('../models/orderModel') -var mongoose = require('mongoose'); - -var order = {} - -order.orderdb = function(req,res) { - newOrd = new ord({name: req.query.checked, price: req.query.allPrice}); - newOrd.save(function (err) { - if (err) return console.error(err); - }); - - vas = {name:req.query.checked, price: req.query.allPrice}; - res.send(vas); -} - -order.kitchen = function(req,res) { - var callback = function(req, res){ - return function(err, orders){ - if (err){ - console.log('error occured'); - return; - }; - res.render("kitchen", {"orders": orders}); - }; - } - ord.find(callback(req, res)) -} - -order.totals = function(req,res) { - res.send(req.query.total) -} - -order.comp = function(req, res) { - - ord.findOneAndRemove({_id: req.query.name}, {_id: req.query.name}, function(err, cats) { - if (err) { - res.status(500).send("Something broke!"); - }; - }); - res.send(req.query.name); -} - - - -module.exports = order; \ No newline at end of file diff --git a/twoter/routes/twote.js b/twoter/routes/twote.js index a0e54848..9b613304 100644 --- a/twoter/routes/twote.js +++ b/twoter/routes/twote.js @@ -11,11 +11,15 @@ db.on('error', console.error.bind(console, 'connection error:')); var twote = {} twote.postLogin = function(req, res) { - sess = req.session; - var sess = req.session; - sess.username = "fuck"; - res.redirect('/'); -} + // I see no reason to define a "sess" variable -- fine to work with req.session directly + // Also, not sure how intentional this was: there were routes which used "sess" (something like console.log(sess.user)) + // *without* defining sess locally. Those references only worked out because, in various routes in this file, you + // defined "sess" without "var" in front -- so, "sess" got scoped globally (to the whole file) instead of + // locally (to just the route handler function). If you'd used "var sess = ..." every time you defined "sess", those routes which + // use "sess" but *don't* define it would have thrown errors along the lines of "sess is undefined". + req.session.username = "fuck"; + res.redirect('/'); +}; twote.logFor = function(accessToken, refreshToken, profile, done) { @@ -40,64 +44,43 @@ twote.logFor = function(accessToken, refreshToken, profile, done) { }); } }); - } + } // again with the indentation -- this should work out to be at the outermost level, if all of your curly braces match up (which it looks like they do) twote.passLogin = function(req, res) { - - var sess = req.session; - sess.username = "fuck"; - // sess.username = req.query.name; - console.log(sess.username); - res.redirect('/'); -} + req.session.username = "fuck"; // I always find it funny to see what strings people use when debugging :) + res.redirect('/'); +}; -// //working +// I'm getting confused in here with the spacing/bracketing inconsistencies -- organizing so I understand what's happening. +// It's also my personal preference to define callbacks inline as anonymous functions -- doing it that way because I find it easier to understand; you can decide what you prefer +// I'm really happy to hear that getting the async calls to work was gratifying :) twote.getUsers = function(req, res){ - // var sess = req.session; - sess = req.session; - - console.log(sess); - if (!sess.username) { - console.log("only this"); - res.redirect("/login"); - } -else { - var time = new Date(Date.now() + 60000); - sess.cookie.expires = time; - console.log("also this"); - var callback = function(req, res){ - return function(err, users){ + if (!req.session.username) { + res.redirect("/login"); + } else { + req.session.cookie.expires = new Date(Date.now() + 60000); // can do this inline -- you never use time again + + usr.find(function(err, users) { if (err){ - console.log('error occured'); - return; + // can do all on one line like this, if you want + // also, error handling! 500 means "internal server error" -- usually pretty appropriate; you can look up other codes if you want + return res.status(500).json(err); }; - var callback2 = function(req, res){ - return function(err, posts){ - if (err){ - console.log('error occured'); - return; + + // The second callback didn't need req and res as params the way your code was, and it still doesn't the way I've + // organized things. req and res are defined in the scope of the route handler -- and that scope includes both callbacks! + post.find({}, null, {sort: '-created'}, function(err, posts) { + if (err){ + return res.status(500).json(err); }; var results = {"user": users, "post": posts} - //res.render("home", {"user": users}); - res.render("home", {"result": results}); - }; - } - - //res.render("home", {"user": users}); - // post.find({}, {sort: {created: -1}}, callback2(req, res)) - post.find({}, null, {sort: '-created'}, callback2(req, res)) - // Room.find({}, null, {sort: '-date'}, function(err, docs) - - //res.render("home", {"user": users}); - }; - - //post.find(callback2(req, res)) + res.render("home", {"result": results}); // Is the object-inside-an-object necessary? + }); + }); } - usr.find(callback(req, res)) -} }; twote.login = function(req, res){ @@ -118,20 +101,20 @@ twote.addUser = function(req, res) { newUsr.save(function (err) { if (err) return console.error(err); }); - + res.send(req.query.name); } twote.logout = function(req,res) { - sess.username = undefined; - usr.update({logged: true},{$set:{logged: false}}, function(err, cats) { - if (err) { + req.session.username = undefined; + usr.update({logged: true},{$set:{logged: false}}, function(err, cats) { // ...cats? weird users you've got + if (err) {- res.status(500).send("Something broke!"); }; res.send(cats); }); - + } twote.getLogged = function(req, res) { @@ -153,7 +136,7 @@ twote.addPost = function(req, res) { newPost.save(function (err) { if (err) return console.error(err); }); - + vas = {author:req.query.author, post: req.query.post}; //res.render("home", {"loggedIn": req.query.name}); @@ -178,7 +161,7 @@ twote.removePost = function(req, res) { } } - if(log == true) { + if(log == true) { post.findOneAndRemove({post: req.query.post}, {author: req.query.author}, function(err, cats) { if (err) { res.status(500).send("Something broke!"); @@ -193,7 +176,7 @@ twote.removePost = function(req, res) { }; usr.find({ 'logged': true },callback(req, res)) } - -module.exports = twote; \ No newline at end of file + +module.exports = twote;