diff --git a/controllers/auth.js b/controllers/auth.js index 43f893aed..3b759a592 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -1,8 +1,12 @@ const passport = require("passport"); +//PACKAGE TO HANDLE AUTHENTICATION. IT HAS STRATEGIES ( DIFFERENT LOGIN METHODS)TO DO DIFFERENT STUFF. IF WE WANTED TO, WE CAN USE LOCAL STRATEGIES OR GOOGLEAUTH ETC. const validator = require("validator"); +//PACKAGE TO VALIDATE THE USER const User = require("../models/User"); +//GET THE USER FROM MODELS FOLDER AND USER FILE (THE BCRYPT FILE) exports.getLogin = (req, res) => { + //IF THE USER TAKEN FROM THE REQUEST INFORMATION, RETURN A RESPOND TO REDIRECT THE USER TO THE /TODO PATH AND RENDER LOGIN if (req.user) { return res.redirect("/profile"); } @@ -12,20 +16,25 @@ exports.getLogin = (req, res) => { }; exports.postLogin = (req, res, next) => { + //Checks whether the email provided in the request body is a valid email using validator.isEmail(). If it’s invalid, it pushes an error message into the validationErrors array. const validationErrors = []; if (!validator.isEmail(req.body.email)) validationErrors.push({ msg: "Please enter a valid email address." }); + // checks if the password is empty using validator.isEmpty(). If the password field is blank, it adds an error message to the validationErrors array. if (validator.isEmpty(req.body.password)) validationErrors.push({ msg: "Password cannot be blank." }); + // If any validation errors are found (i.e., the validationErrors array has items), it flashes the errors to the session using req.flash('errors', validationErrors) and redirects the user back to the login page using res.redirect('/login'). This allows the user to see the error messages and correct their input. if (validationErrors.length) { req.flash("errors", validationErrors); return res.redirect("/login"); } + // If the input passes the validation, the email is normalized using validator.normalizeEmail(). req.body.email = validator.normalizeEmail(req.body.email, { gmail_remove_dots: false, }); + //AUTH THE USER USING PASSPORT LOCAL STRAEGY, IF ERROR PROCEED TO ERROR, IF NOT USER, USING FLASH, SAY USER AND PROVIDE INFO passport.authenticate("local", (err, user, info) => { if (err) { return next(err); @@ -34,20 +43,24 @@ exports.postLogin = (req, res, next) => { req.flash("errors", info); return res.redirect("/login"); } + //IF THE REQUEST INFO PASSED TO LOGIN FUNCTION IS SUCCESSFUL, USEFLASH TO NOTIFY A MESSAGE req.logIn(user, (err) => { if (err) { return next(err); } req.flash("success", { msg: "Success! You are logged in." }); + //REDIRECT THE USER TO THE TODO PATH res.redirect(req.session.returnTo || "/profile"); }); })(req, res, next); }; +//CHECK THE INFO PASSED IN THE REQ BODY AND USE THE LOGOUT FUNC TO CONSOLE.LOG A MESSAGE exports.logout = (req, res) => { req.logout(() => { console.log('User has logged out.') }) + //IN THE REQ BODE, DESTROY THE SESSION AND REDIRECT THE USER TO MAIN PAGE TO LOG BACK IN req.session.destroy((err) => { if (err) console.log("Error : Failed to destroy the session during logout.", err); @@ -56,25 +69,31 @@ exports.logout = (req, res) => { }); }; +//IN THE REQ BODY OF THE USER RES WITH REDIRECT TO TODOS exports.getSignup = (req, res) => { if (req.user) { return res.redirect("/profile"); } + //RENDER SIGNUP AND TITLE res.render("signup", { title: "Create Account", }); }; +//Checks whether the email provided in the request body is a valid email using validator.isEmail(). If it’s invalid, it pushes an error message into the validationErrors array. exports.postSignup = (req, res, next) => { const validationErrors = []; if (!validator.isEmail(req.body.email)) validationErrors.push({ msg: "Please enter a valid email address." }); + //CHECK IF THE LEGTH OF THE PASSWORK FROM THE REQ BODY IS MIN 8 CHARACTER LONG, IF ITS NOT RETURN A MESSAGE if (!validator.isLength(req.body.password, { min: 8 })) validationErrors.push({ msg: "Password must be at least 8 characters long", }); + //IF THE PASSWORDS DONT MATCH, RETURN A MESSAGE if (req.body.password !== req.body.confirmPassword) validationErrors.push({ msg: "Passwords do not match" }); + // IF THE LENGTH ISNT VALIDATED, USE FLASH TO SHOW ERRORS AND REDIRECT TO SIGNUP ROUTE. if (validationErrors.length) { req.flash("errors", validationErrors); @@ -84,12 +103,14 @@ exports.postSignup = (req, res, next) => { gmail_remove_dots: false, }); + //CREATE A NEW USER USING THE REQ BODY SUBMITTED FROM THE FORM OF THE USER AND SAVE IT AS A VAR OF USER. const user = new User({ userName: req.body.userName, email: req.body.email, password: req.body.password, }); + // FIND THE USER, USING THE EMAIL AND USERNAME FROM THE REQU BODY AND IF THE USER ALREADY EXIST, USE FLASH TO INSERT AN ERROR User.findOne( { $or: [{ email: req.body.email }, { userName: req.body.userName }] }, (err, existingUser) => { diff --git a/controllers/comments.js b/controllers/comments.js new file mode 100644 index 000000000..6028edc90 --- /dev/null +++ b/controllers/comments.js @@ -0,0 +1,51 @@ +const Comment = require("../models/Comment"); +//LINK THE COMMENT MODEL FROM THE MODEL FOLDER AND COMMENT FILE TO USE IN THE FOLLOWING CODE + +module.exports = { + //FIND THE POST FROM THE SPECIFIC USERID FROM THE REQ BODY AND RENDER THE POST AND USER IN THE PROFILE.EJS + + createComment: async (req, res) => { + try { + await Comment.create({ + comment: req.body.comment, + post: req.params.id, + likes: 0, + user: req.user.id, + userName: req.body.userName, + }); + console.log("Comment has been added!"); + res.redirect("/post/"+req.params.id); + } catch (err) { + console.log(err); + } + }, + //FIND AND UPDATE THE LIKES ON A POST IN THE POST COLLECTION BY USING THE ID FROM THE REQ.PARAMS.ID AND THEN INCREMENT THE LIKES BY 1. AND THE REDIRECT THE USER TO THE POST FROM THE SPECIFIED ID + likeComment: async (req, res) => { + try { + await Comment.findOneAndUpdate( + { _id: req.params.id }, + { + $inc: { likes: 1 }, + } + ); + console.log("Likes +1"); + res.redirect(`/post/${req.params.id}`); + } catch (err) { + console.log(err); + } + }, + //FIND THE IDFROM THE REQ PARAMS ID. DELETE THE IMAGE FROM THE CLOUDINARY USING THE CLOUDINARY ID, DELETE THE POST FROM THE DB USING THE PARAMS ID, THEN REDIRECT THE USER TO THE PROFILE PAGE + deleteComment: async (req, res) => { + try { + // Find post by id + let comment = await Comment.findById({ _id: req.params.id }); + + // Delete post from db + await Comment.remove({ _id: req.params.id }); + console.log("Deleted Comment"); + res.redirect("/post/"+req.params.id); + } catch (err) { + res.redirect("/post/"+req.params.id); + } + }, +}; diff --git a/controllers/posts.js b/controllers/posts.js index a3e2dab5d..630da3e23 100644 --- a/controllers/posts.js +++ b/controllers/posts.js @@ -1,7 +1,13 @@ const cloudinary = require("../middleware/cloudinary"); +//TO SHOW THE IMAGES IN THE POST, LINK THE CLOUDINARY FROM THE MIDDLEWARE FOLDER const Post = require("../models/Post"); +//LINK THE POST MODEL FROM THE MODEL FOLDER AND POST FILE TO USE IN THE FOLLOWING CODE +const Comment = require("../models/Comment"); +//LINK THE COMMENT MODEL FROM THE MODEL FOLDER AND COMMENT FILE TO USE IN THE FOLLOWING CODE + module.exports = { + //FIND THE POST FROM THE SPECIFIC USERID FROM THE REQ BODY AND RENDER THE POST AND USER IN THE PROFILE.EJS getProfile: async (req, res) => { try { const posts = await Post.find({ user: req.user.id }); @@ -10,6 +16,7 @@ module.exports = { console.log(err); } }, + //FIND THE POST FROM THE DB AND SORT IT FROM DESCINDING ORDER AND ONLY GET THE SHORT VERSION OF THE REQ INSTEAD OF THE FULL REQ. THEN RENDER THE POST AND USER IN THE FEED.EJS getFeed: async (req, res) => { try { const posts = await Post.find().sort({ createdAt: "desc" }).lean(); @@ -18,14 +25,21 @@ module.exports = { console.log(err); } }, + //FIND THE id IN THE DB FROM THE PARAMS ID THEN RENDER THE POST AND USER IN THE POST.EJS + //use the comment model to goto the comment collection to find all the post associated with the current post that you are actual on, sort them in desc order and shorten the info sent back. + //then render the comment to ejs getPost: async (req, res) => { try { const post = await Post.findById(req.params.id); - res.render("post.ejs", { post: post, user: req.user }); + + const comments = await Comment.find({post: req.params.id}).sort({ createdAt: "desc" }).lean(); + + res.render("post.ejs", { post: post, user: req.user, comments: comments }); } catch (err) { console.log(err); } }, + //CREATE A POST TO THE DB BY ADDING THE IMAGE TO CLOUDNAIRY FROM THE FILE.PATH THEN CREATE A POST IN THE DB BY SAVING THE TITLE(FROM THE REQ BODY) , IMAGE(A URL FROM CLOUDINARY), CLOUDINARY ID(FROM CLOUDINARY AND USEFUL FOR DELTETING THE IMAGE) CAPTION(FROM THE REQ BODY) AND LIKES(SET TO 0) AND USER (FROM THE REQ BODY) AND REDIRECT THE USER TO THE PROFILE PAGE createPost: async (req, res) => { try { // Upload image to cloudinary @@ -45,6 +59,7 @@ module.exports = { console.log(err); } }, + //FIND AND UPDATE THE LIKES ON A POST IN THE POST COLLECTION BY USING THE ID FROM THE REQ.PARAMS.ID AND THEN INCREMENT THE LIKES BY 1. AND THE REDIRECT THE USER TO THE POST FROM THE SPECIFIED ID likePost: async (req, res) => { try { await Post.findOneAndUpdate( @@ -59,6 +74,7 @@ module.exports = { console.log(err); } }, + //FIND THE IDFROM THE REQ PARAMS ID. DELETE THE IMAGE FROM THE CLOUDINARY USING THE CLOUDINARY ID, DELETE THE POST FROM THE DB USING THE PARAMS ID, THEN REDIRECT THE USER TO THE PROFILE PAGE deletePost: async (req, res) => { try { // Find post by id diff --git a/middleware/auth.js b/middleware/auth.js index f646630da..91601e00e 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -1,3 +1,5 @@ +// THE ENSUREAUTH FUNCTION WILL AUTH THE REQUEST AND AALLOW YOU TO GO NEXT ELSE IF NOT AUTH RESUDIRECT TO MAIN ROUTE. If the user is not authenticated , ET THEM GO ON TO THE NEXT ROUTE. IF THEY ARE AUTHENTICAATED, REDIRECT THEM TO THE DASHBOARD + module.exports = { ensureAuth: function (req, res, next) { if (req.isAuthenticated()) { diff --git a/middleware/cloudinary.js b/middleware/cloudinary.js index 0960c5b6f..cec54a196 100644 --- a/middleware/cloudinary.js +++ b/middleware/cloudinary.js @@ -1,6 +1,8 @@ const cloudinary = require("cloudinary").v2; +//USE CLOUDINARE V2 require("dotenv").config({ path: "./config/.env" }); +//ALLOW EXPRESS TO USE ENV TO GET ACCES TO CLOUDINARY SECRETS cloudinary.config({ cloud_name: process.env.CLOUD_NAME, diff --git a/middleware/multer.js b/middleware/multer.js index c012afe58..d5aaf8d7b 100644 --- a/middleware/multer.js +++ b/middleware/multer.js @@ -1,11 +1,16 @@ const multer = require("multer"); +//USED FOR UPLOADING IMAGES const path = require("path"); +//Imports the path module, which provides utilities for working with file and directory paths. + +//Exports a configured instance of multer so it can be used elsewhere in your app. module.exports = multer({ - storage: multer.diskStorage({}), + storage: multer.diskStorage({}),//Specifies the storage engine for uploaded files. fileFilter: (req, file, cb) => { - let ext = path.extname(file.originalname); - if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") { + let ext = path.extname(file.originalname);//Extracts the file extension (e.g., .jpg, .png) from the original file name of the uploaded file using path.extname. + if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png")//Checks if the file extension is not one of the supported types (.jpg, .jpeg, .png). + { cb(new Error("File type is not supported"), false); return; } diff --git a/models/Comment.js b/models/Comment.js new file mode 100644 index 000000000..6c2be200b --- /dev/null +++ b/models/Comment.js @@ -0,0 +1,34 @@ +const mongoose = require("mongoose"); +////USE MONGOOSE TO TALK TO MONGODB DB AND GIVE US A SCHEMA TO USE + +const CommentSchema = new mongoose.Schema({ + //HOW TO TAKE THE DATA FROM FORM TO PUT INTO DB + comment: {// ADD comment TO THE DB AS A STRING/SENTENCE AND ITS REQUIRED + type: String, + required: true, + }, + likes: { + // ADD LIKES TO THE DB AS A NUMBER AND ITS REQUIRED + type: Number, + required: true, + }, + user: { + //MONGO WILL CREATE A UNIQUE USERID + type: mongoose.Schema.Types.ObjectId, + ref: "User", + }, + userName: { + type: String, unique: true }, + post: { + //MONGO WILL CREATE A UNIQUE PostID + type: mongoose.Schema.Types.ObjectId, + ref: "UserName", + }, + createdAt: { + ////MONGO WILL CREATE A UNIQUE DATE WITH THE TIME (UTC) + type: Date, + default: Date.now, + }, +}); + +module.exports = mongoose.model("Comment", CommentSchema); diff --git a/models/Post.js b/models/Post.js index f7d14c981..b902bb430 100644 --- a/models/Post.js +++ b/models/Post.js @@ -1,31 +1,38 @@ const mongoose = require("mongoose"); +////USE MONGOOSE TO TALK TO MONGODB DB AND GIVE US A SCHEMA TO USE const PostSchema = new mongoose.Schema({ - title: { + //HOW TO TAKE THE DATA FROM FORM TO PUT INTO DB + title: {// ADD TITLE TO THE DB AS A STRING/SENTENCE AND ITS REQUIRED type: String, required: true, }, - image: { + image: {// ADD IMAGE URL TO THE DB AS A STRING AND ITS REQUIRED type: String, require: true, }, cloudinaryId: { + // ADD CLOUDINARYID TO THE DB AS A STRING/SENTENCE AND ITS REQUIRED type: String, require: true, }, caption: { + // ADD CAPTION TO THE DB AS A STRING/SENTENCE AND ITS REQUIRED type: String, required: true, }, likes: { + // ADD LIKES TO THE DB AS A NUMBER AND ITS REQUIRED type: Number, required: true, }, user: { + //MONGO WILL CREATE A UNIQUE USERID type: mongoose.Schema.Types.ObjectId, ref: "User", }, createdAt: { + ////MONGO WILL CREATE A UNIQUE DATE WITH THE TIME (UTC) type: Date, default: Date.now, }, diff --git a/models/User.js b/models/User.js index afe8afc36..f9334d852 100644 --- a/models/User.js +++ b/models/User.js @@ -1,7 +1,10 @@ const bcrypt = require("bcrypt"); +//// USE THE BCRYPT MODULE TO ENCRYPT YOUR PASWWORD const mongoose = require("mongoose"); +//USE MONGOOSE TO INTERACT WITH MONGO DB AND ALLOW US TO USE SCHEMA const UserSchema = new mongoose.Schema({ + //CREATE A SCHEMA USER WHERE USER,EMAIL AND PASSWORD IS A STRING AND UNQIUE userName: { type: String, unique: true }, email: { type: String, unique: true }, password: String, @@ -40,3 +43,4 @@ UserSchema.methods.comparePassword = function comparePassword( }; module.exports = mongoose.model("User", UserSchema); +//EXPORT THE USER AND USERSCHEMA diff --git a/routes/comments.js b/routes/comments.js new file mode 100644 index 000000000..3105b7bb4 --- /dev/null +++ b/routes/comments.js @@ -0,0 +1,23 @@ +//USE EXPRESS TO MAKE NODEJS EASIER +const express = require("express"); +//get a new router instance AND USE APP.USE LATER TO CALL THE ROUTER VAR +const router = express.Router(); +//import the configuration in the multer.js located in the middleware folder. The multer allows you to upload images + +const commentsController = require("../controllers/comments"); +//CONNECT ENSUREAUTH FROM FOLDER OF MIDDLEWARE AND FILE OF AUTH AND ITS USED TO MAKE SURE THE USE IS ALWAYS LOGGED IN. WE USE THE CURLY BRACKETS AS DESTRUCTORS BUT IT COULD BE CHANGED TO GUEST ACCTS OR OTHERS USERS. +const { ensureAuth, ensureGuest } = require("../middleware/auth"); + +//Comments Routes - simplified for now + + +router.post("/createComment/:id", commentsController.createComment); +////IF THERES A POST REQUEST ON THE CreatePOST ROUTE, THE ROUTER WILL USE THE MULTER.JS FUNC TO UPLOAD A FILE AND THE POSTCONTROLLER WILL USE CREATEPOST FUNCTION + +router.put("/likeComment/:id", commentsController.likeComment); +//IF THERES A PUT REQUEST ON THE LIKEpost ROUTE WITH THE PARAM OF /:ID FOR A SPECIFIC POST, THE ROUTER WILL USE THE POSTCONTROLLER WILL USE LIKEPOST FUNCTION + +router.delete("/deleteComment/:id", commentsController.deleteComment); +//IF THERES A DELETE REQUEST ON THE DELETEPOST ROUTE WITH THE PARAM OF /:ID FOR A SPECIFIC POST, THE ROUTER WILL USE THE POSTCONTROLLER WILL USE DELETEPOST FUNCTION + +module.exports = router; diff --git a/routes/main.js b/routes/main.js index d6883000e..ab0a621bf 100644 --- a/routes/main.js +++ b/routes/main.js @@ -1,18 +1,31 @@ +// Use express to make using nodejs easier const express = require("express"); +//get a new router instance AND USE APP.USE LATER TO CALL THE ROUTER VAR const router = express.Router(); +//CONNECT AUTH FROM FOLDER OF CONTROLLER AND FILE OF AUTH const authController = require("../controllers/auth"); +//CONNECT HOME CONTROLLER FROM FOLDER OF CONTROLLER AND FILE OF HOME const homeController = require("../controllers/home"); +//CONNECT POST CONTROLLER FROM FOLDER OF CONTROLLER AND FILE OF posts const postsController = require("../controllers/posts"); +//CONNECT ENSUREAUTH FROM FOLDER OF MIDDLEWARE AND FILE OF AUTH AND ITS USED TO MAKE SURE THE USE IS ALWAYS LOGGED IN. WE USE THE CURLY BRACKETS AS DESTRUCTORS BUT IT COULD BE CHANGED TO GUEST ACCTS OR OTHERS USERS. const { ensureAuth, ensureGuest } = require("../middleware/auth"); //Main Routes - simplified for now router.get("/", homeController.getIndex); +//IF THE ROUTER GETS A "GET" REQUEST ON THE MAIN ROUTE, GO TO THE HOMECONTROLLER AND USE THE GETINDEX FUNCTION. REMEMBER TO GO TO THE CONTROLLER FOLDER AND HOME FILE TO GET THE FUNC router.get("/profile", ensureAuth, postsController.getProfile); +//If the router gets a "GET" request on the profile route, ensure that the user is authenticated and then got to the postcontroller controller and go to the getprofile function router.get("/feed", ensureAuth, postsController.getFeed); +//If the router gets a "GET" request on the feed route, ensure that the user is authenticated and then got to the postcontroller controller and go to the getFeed function router.get("/login", authController.getLogin); +//If the router gets a "GET" request on the login route, go to the authController and go to the getLogin function router.post("/login", authController.postLogin); +//If the router gets a "POST" request on the login route, go to the authController and go to the postLogin function router.get("/logout", authController.logout); +//If the router gets a "GET" request on the logout route, go to the authController and go to the logout function router.get("/signup", authController.getSignup); +//If the router gets a "GET" request on the signup route, go to the authController and go to the getSignup function router.post("/signup", authController.postSignup); - +//If the router gets a "POST" request on the signup route, go to the authController and go to the postSignup function module.exports = router; diff --git a/routes/posts.js b/routes/posts.js index aa463ac90..3ef59ea85 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -1,16 +1,23 @@ +//USE EXPRESS TO MAKE NODEJS EASIER const express = require("express"); +//get a new router instance AND USE APP.USE LATER TO CALL THE ROUTER VAR const router = express.Router(); +//import the configuration in the multer.js located in the middleware folder. The multer allows you to upload images const upload = require("../middleware/multer"); +//import your postcontroller by going to controller and post file const postsController = require("../controllers/posts"); +//CONNECT ENSUREAUTH FROM FOLDER OF MIDDLEWARE AND FILE OF AUTH AND ITS USED TO MAKE SURE THE USE IS ALWAYS LOGGED IN. WE USE THE CURLY BRACKETS AS DESTRUCTORS BUT IT COULD BE CHANGED TO GUEST ACCTS OR OTHERS USERS. const { ensureAuth, ensureGuest } = require("../middleware/auth"); //Post Routes - simplified for now -router.get("/:id", ensureAuth, postsController.getPost); +router.get("/:id", ensureAuth, postsController.getPost); ////IF THERES A GET REQUEST ON THE MAIN PAGE with the params of id of the post, THE ROUTER WILL USE THE ENSUREAUTH FUNC AND IF ITS APPROVED, THE POSTCONTROLLER WILL USE GETPOST FUNCTION router.post("/createPost", upload.single("file"), postsController.createPost); +////IF THERES A POST REQUEST ON THE CreatePOST ROUTE, THE ROUTER WILL USE THE MULTER.JS FUNC TO UPLOAD A FILE AND THE POSTCONTROLLER WILL USE CREATEPOST FUNCTION router.put("/likePost/:id", postsController.likePost); +//IF THERES A PUT REQUEST ON THE LIKEpost ROUTE WITH THE PARAM OF /:ID FOR A SPECIFIC POST, THE ROUTER WILL USE THE POSTCONTROLLER WILL USE LIKEPOST FUNCTION router.delete("/deletePost/:id", postsController.deletePost); - +//IF THERES A DELETE REQUEST ON THE DELETEPOST ROUTE WITH THE PARAM OF /:ID FOR A SPECIFIC POST, THE ROUTER WILL USE THE POSTCONTROLLER WILL USE DELETEPOST FUNCTION module.exports = router; diff --git a/server.js b/server.js index 1718db010..01b922d88 100644 --- a/server.js +++ b/server.js @@ -1,40 +1,55 @@ -const express = require("express"); +const express = require("express");//USE EXPRESS TO MAKE USING NODEJS EASIER LOL const app = express(); +//WRAP THE EXPRESS FUNCTION IN APP VAR const mongoose = require("mongoose"); +//USE MONGOOSE TO TALK TO MONGODB EASIER const passport = require("passport"); +////USE PASSPORT PACKAGE FOR AUTH const session = require("express-session"); +// USE SESSION PACKAGE TO HANDLE THE COOKIES ON THE USER COMPUTER THAT MATCHES THE SESSIONS STORED IN OUR DB SO THE USER CAN STAY LOGGED IN const MongoStore = require("connect-mongo")(session); +// USE SESSION PACKAGE TO HANDLE THE COOKIES ON THE USER COMPUTER THAT MATCHES THE SESSIONS STORED IN OUR DB SO THE USER CAN STAY LOGGED IN const methodOverride = require("method-override"); +//overrides the HTTP method of a request if a specific parameter or header is present. const flash = require("express-flash"); +//GIVE US A NOTIFICATION IN CASE WE LOGIN WRONG OR SIGNUP USING WRONG INFORMATION const logger = require("morgan"); +//DEBUGGER AND WILL KEEP THE LOG OF EVERY REQ THAT COMES THRU INSTEAD OF CONSOLE LOG EVER LINE OF CODE const connectDB = require("./config/database"); +//CONNECT TO DB USING CONFIG FOLDER AND DB FILE const mainRoutes = require("./routes/main"); +//LINK TO OUR MAIN ROUTE const postRoutes = require("./routes/posts"); +//LINK TO OUR POST ROUTE +const commentRoutes = require("./routes/comments"); +//LINK TO OUR POST ROUTE //Use .env file in config folder require("dotenv").config({ path: "./config/.env" }); +/// TELL EXPRESS TO USE OUR ENV VAR. WE CAN USE BUN INSTEAD WHICH HAS ENV VAR BUILT IN // Passport config require("./config/passport")(passport); +//TELLS EXPRESS TO USE PASSPORT //Connect To Database -connectDB(); +connectDB();//FUNC TO CONNECT TO OUR DB //Using EJS for views -app.set("view engine", "ejs"); +app.set("view engine", "ejs");//SET THE VIEW ENGINE AS EJS //Static Folder -app.use(express.static("public")); +app.use(express.static("public"));//HANDLES THE CSS AND JS //Body Parsing app.use(express.urlencoded({ extended: true })); -app.use(express.json()); +app.use(express.json());// ENABLES US TO LOOK AT THE REQ BODY COMING FROM THE FORM AND OULL WHAT WE NEED FROM IT //Logging -app.use(logger("dev")); +app.use(logger("dev"));//HELPS US TO SET UP MORGAN AND LOG EVERYTHING //Use forms for put / delete -app.use(methodOverride("_method")); +app.use(methodOverride("_method"));//IF THE _METHOD EXISIT THEN THE MIDDLEWARE WILL USE METHODOVERRIDE TO OVERRIDE THE ORGINAL METHOD OF PUT, DELETE, POST // Setup Sessions - stored in MongoDB app.use( @@ -47,17 +62,23 @@ app.use( ); // Passport middleware -app.use(passport.initialize()); -app.use(passport.session()); +app.use(passport.initialize());//SETUP PASSPORT AS AUTH +app.use(passport.session());//THEN USE SESSIONS ALONG WITH PASSPORT //Use flash messages for errors, info, ect... app.use(flash()); +//SETUP FLASH FOR ALERTS //Setup Routes For Which The Server Is Listening -app.use("/", mainRoutes); +app.use("/", mainRoutes);//REQUEST ON THE MAIN ROUTE, USE THE MAINROUTE IN THE ROUTE FOLDER app.use("/post", postRoutes); +//REQ ON THE POST ROUTE, USE THE POSTROUTES FOLDER. +app.use("/comment", commentRoutes); +//REQ ON THE COMMENT ROUTE, USE THE COMMENTROUTES FOLDER. + //Server Running app.listen(process.env.PORT, () => { console.log("Server is running, you better catch it!"); + //HAVE THE SERVER LISTEN ON THE SET PORT OR WHATEVER PORT ISSUED BY ENV }); diff --git a/views/post.ejs b/views/post.ejs index c36a1c946..43dac012f 100644 --- a/views/post.ejs +++ b/views/post.ejs @@ -4,6 +4,13 @@
<%= post.caption %>
+<%= post.caption %>
+ +