Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //====app.js====/
- require('dotenv').config();
- var express = require("express");
- var app = express();
- var mongoose = require("mongoose");
- var bodyParser = require("body-parser");
- var flash = require("connect-flash");
- var Mail = require("./models/mail");
- var Blog = require("./models/blogpost");
- var Comment = require("./models/comments");
- var methodOverride = require("method-override");
- var indexRoutes = require("./routes/index");
- var contactRoutes = require("./routes/mail");
- var blogRoutes = require("./routes/blog");
- var commentRoutes = require("./routes/comments");
- //-------------//
- // APP CONFIG//
- //-------------//
- mongoose.connect(process.env.DATABASEURL)
- .then(connection => {
- console.log('Connected to MongoDB')
- })
- .catch(error => {
- console.log(error.message)
- });
- app.use(bodyParser.urlencoded({extended:true}));
- app.set("view engine", "ejs");
- app.use(express.static(__dirname + "/public"));
- app.use(methodOverride("_method"));
- app.use(flash());
- app.use(require("express-session")({
- secret: "My name is Saint Chuks",
- resave: false,
- saveUninitialized: false
- }));
- app.use(function(req, res, next){
- res.locals.error = req.flash("error");
- res.locals.success = req.flash("success");
- next();
- });
- app.use(indexRoutes);
- app.use("/contact", contactRoutes);
- app.use("/blog", blogRoutes);
- app.use("/blog/:id/comments");
- app.listen(process.env.PORT, process.env.IP, function(){
- console.log("Portfolio server started!!");
- });
- //====Routes======//
- //blog.js
- var express = require("express");
- var router = express.Router();
- var Blog = require("../models/blogpost");
- var multer = require("multer");
- var async = require("async");
- //Multer Storage
- var storage = multer.diskStorage({
- filename: function(req, file, callback){
- callback(null, Date.now()+ file.originalname);
- }});
- //Multer Filter
- var imageFilter = function(req, file, cb){
- // accept only image files
- if(!file.originalname.match(/\.(jpg|jpeg|png|gif)$/i)){
- cb(new Error('only image files are allowed'), false);
- }
- cb(null, true);
- };
- //Storing Image + Filter
- var upload = multer({storage: storage, fileFilter: imageFilter});
- //Cloudinary Configuration
- var cloudinary = require("cloudinary");
- cloudinary.config({
- cloud_name: 'dnposhqpc',
- api_key: process.env.CLOUDINARY_API_KEY,
- api_secret: process.env.CLOUDINARY_API_SECRET
- });
- //------Routes----
- //---------------/
- // INDEX ROUTE TO SHOW ALL BLOG POST
- router.get("/", function(req, res) {
- Blog.find({}, function(err, blogs){
- if(err){
- console.log("Error");
- } else {
- res.render('blog/index', {blogs: blogs});
- }
- });
- });
- // New Route---/
- router.get("/new", function(req, res){
- res.render("blog/new");
- });
- //Create New Blog
- router.post("/", upload.single('image'), function(req, res){
- var title = req.body.title;
- var image = req.body.image;
- var imageId = req.body.imageId;
- var body = req.body.body;
- cloudinary.uploader.upload(req.file.path, function(result){
- image = result.secure_url;
- imageId = result.public_id;
- var newBlog = {title: title, image: image, imageId: imageId, body: body};
- Blog.create(newBlog, function(err, newpost){
- if(err || !newpost){
- console.log('error');
- } else {
- console.log('newpost');
- res.redirect("/blog");
- }
- });
- });
- })
- // SHOW ROUTE
- router.get("/:id", function(req, res) {
- Blog.findById(req.params.id, function(err, foundBlog){
- if(err){
- console.log('error');
- res.redirect("/blog");
- } else {
- res.render("blog/show", {blog: foundBlog});
- }
- });
- });
- // EDIT ROUTE
- router.get("/:id/edit", function(req, res) {
- Blog.findById(req.params.id, function(err, foundBlog){
- if(err) {
- console.log('error');
- res.redirect("/blog");
- } else {
- res.render("blog/edit", {blog: foundBlog});
- }
- });
- });
- // UPDATE ROUTE
- router.put("/:id", upload.single('image'), function(req, res){
- Blog.findById(req.params.id, async function(err, foundBlog){
- if(err){
- req.flash("error", "Blog ID not found");
- return res.redirect("back");
- } else {
- if(req.file){
- try {
- await cloudinary.v2.uploader.destroy(foundBlog.imageId);
- var result = await cloudinary.v2.uploader.upload(req.file.path);
- foundBlog.imageId = result.public_id;
- foundBlog.image = result.secure_url;
- } catch(err){
- req.flash("error", "Blog ID not found");
- return res.redirect("back");
- }
- }
- foundBlog.title = req.body.title;
- foundBlog.body = req.body.body;
- foundBlog.save();
- req.flash("success","Successfully Updated!");
- res.redirect("/blog/" + foundBlog._id);
- }
- });
- });
- // DELETE ROUTE
- router.delete("/:id", function(req, res){
- Blog.findById(req.params.id, async function(err, foundBlog) {
- if(err) {
- req.flash("error", "Blog ID not found");
- return res.redirect("back");
- }
- try {
- await cloudinary.v2.uploader.destroy(foundBlog.imageId);
- foundBlog.remove();
- req.flash("success","Successfully Updated!");
- res.redirect("/blog");
- } catch(err) {
- if(err) {
- req.flash("error", "Blog ID not found");
- return res.redirect("back");
- }
- }
- });
- });
- module.exports = router;
- //comments.js
- var express = require("express");
- var router = express.Router();
- var Blog = require("../models/blogpost");
- var Comment = require("../models/comments");
- //===============/
- //====ROUTES====/
- // COMMENTS NEW ROUTE
- router.get("/new", function(req, res){
- Blog.findById(req.params.id, function(err, foundblog){
- if(err) {
- console.log("error", "no id found");
- res.redirect("back");
- } else {
- res.render("/commentnew", {foundblog: foundblog});
- }
- });
- });
- module.exports = router;
- //====SCHEMAS===//
- //comments.js
- var mongoose = require("mongoose");
- var commentSchema = mongoose.Schema({
- text: String,
- createdOn: { type: Date, default: Date.now}
- });
- module.exports = mongoose.model('Comment', commentSchema);
- //blogpost.js
- var mongoose = require("mongoose");
- var Blogschema = new mongoose.Schema ({
- image: String,
- imageId: String,
- title: String,
- body: String,
- created: {type: Date, default: Date.now},
- comment: [
- {
- type: mongoose.Schema.Types.ObjectId,
- ref: 'comment'
- }
- ]
- }, {usePushEach: true});
- module.exports = mongoose.model('Blog', Blogschema);
Add Comment
Please, Sign In to add comment