-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921f4d5
commit d4b3619
Showing
9 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
var bodyParser = require("body-parser"), | ||
methodOverride = require("method-override"), | ||
expressSanitizer= require("express-sanitizer"), | ||
mongoose = require("mongoose"), | ||
express = require("express"), | ||
app = express(); | ||
|
||
// APP CONFIG | ||
mongoose.connect("mongodb://localhost/restful_blog_app"); | ||
app.set("view engine", "ejs"); | ||
app.use(express.static("public")); | ||
app.use(bodyParser.urlencoded({extended: true})); | ||
app.use(expressSanitizer()); | ||
app.use(methodOverride("_method")); | ||
|
||
// MONGOOSE/MODEL CONFIG | ||
var blogSchema = new mongoose.Schema({ | ||
title: String, | ||
image: String, | ||
body: String, | ||
created: {type: Date, default: Date.now} | ||
}); | ||
|
||
var Blog = mongoose.model("Blog", blogSchema); | ||
|
||
|
||
// RESTFUL ROUTES | ||
|
||
app.get("/", function(req, res){ | ||
res.redirect("/blogs"); | ||
}); | ||
|
||
// INDEX ROUTE | ||
app.get("/blogs", function(req, res){ | ||
Blog.find({}, function(err, blogs){ | ||
if(err){ | ||
console.log("ERROR!"); | ||
} else { | ||
res.render("index", {blogs: blogs}); | ||
} | ||
}); | ||
}); | ||
|
||
// NEW ROUTE | ||
app.get("/blogs/new", function (req, res){ | ||
res.render("new"); | ||
}); | ||
|
||
//CREATE ROUTE | ||
app.post("/blogs", function(req, res){ | ||
// create blog | ||
console.log(req.body); | ||
req.body.blog.body = req.sanitize(req.body.blog.body); | ||
console.log("==============="); | ||
console.log(req.body); | ||
Blog.create(req.body.blog, function(err, newBlog){ | ||
if(err){ | ||
res.render("new"); | ||
} else { | ||
// redirect to index | ||
res.redirect("/blogs"); | ||
} | ||
}); | ||
}); | ||
|
||
// SHOW ROUTE | ||
app.get("/blogs/:id", function(req, res){ | ||
Blog.findById(req.params.id, function(err, foundBlog){ | ||
if(err){ | ||
res.redirect("/blogs"); | ||
} else { | ||
res.render("show", {blog: foundBlog}); | ||
} | ||
}); | ||
}); | ||
|
||
// EDIT ROUTE | ||
|
||
app.get("/blogs/:id/edit", function(req, res){ | ||
Blog.findById(req.params.id, function(err, foundBlog){ | ||
if(err){ | ||
res.redirect("/blogs"); | ||
} else { | ||
res.render("edit", {blog: foundBlog}); | ||
} | ||
}); | ||
}); | ||
|
||
// UPDATE ROUTE | ||
app.put("/blogs/:id", function(req, res){ | ||
req.body.blog.body = req.sanitize(req.body.blog.body); | ||
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function(err, updatedBlog){ | ||
if(err){ | ||
res.redirect("/blogs"); | ||
} else { | ||
res.redirect("/blogs/" + req.params.id); | ||
} | ||
}); | ||
}); | ||
|
||
// DELETE ROUTE | ||
app.delete("/blogs/:id", function(req, res){ | ||
//destroy blog | ||
Blog.findByIdAndRemove(req.params.id, function(err){ | ||
if(err){ | ||
res.redirect("/blogs"); | ||
} else { | ||
res.redirect("/blogs"); | ||
} | ||
}); | ||
//redirect somewhere | ||
}); | ||
|
||
|
||
app.listen(process.env.PORT, process.env.IP, function(){ | ||
console.log("SERVER IS RUNNING!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "restfulblogapp", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Jacob Dowdy", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.18.2", | ||
"ejs": "^2.5.7", | ||
"express": "^4.16.2", | ||
"express-sanitizer": "^1.0.3", | ||
"method-override": "^2.3.10", | ||
"mongoose": "^5.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
i.icon { | ||
font-size: 2em; | ||
} | ||
|
||
.container.main { | ||
margin-top: 7.0em; | ||
} | ||
|
||
#delete { | ||
display: inline; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<% include ./partials/header %> | ||
|
||
<div class="ui main text container segment"> | ||
<div class="ui huge header">Edit <%= blog.title %></div> | ||
<form class="ui form" action="/blogs/<%= blog._id %>?_method=PUT" method="POST"> | ||
<div class="field"> | ||
<label>Title</label> | ||
<input type="text" name="blog[title]" value="<%= blog.title %>"> | ||
</div> | ||
<div class="field"> | ||
<label>Image</label> | ||
<input type="text" name="blog[image]" value="<%= blog.image %>"> | ||
</div> | ||
<div class="field"> | ||
<label>Blog Content</label> | ||
<textarea name="blog[body]"><%= blog.body %></textarea> | ||
</div> | ||
<input class="ui violet big basic button" type="submit"> | ||
</form> | ||
</div> | ||
|
||
<% include ./partials/footer %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<% include ./partials/header %> | ||
|
||
<h1>INDEX PAGE!!!</h1> | ||
|
||
<div class="ui main text container"> | ||
<div class="ui huge header">RESTful Blog App</div> | ||
<div class="ui top attached segment"> | ||
<div class="ui divided items"> | ||
<% blogs.forEach(function(blog){ %> | ||
<div class="item"> | ||
<div class="image"> | ||
<img src="<%= blog.image %>"> | ||
</div> | ||
<div class="content"> | ||
<a class="header" href="/blogs/<%= blog._id %>"<%= blog.title %></a> | ||
<div class="meta"> | ||
<span><%= blog.created.toDateString() %></span> | ||
</div> | ||
<div class="description"> | ||
<p><%- blog.body.substring(0, 100)%>...</p> | ||
</div> | ||
<div class="extra"> | ||
<a class="ui floated basic violet button" href="/blogs/<%= blog._id %>"><%= blog.title %> | ||
Read More | ||
<i class="right chevron icon"></i> | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<% });%> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<% include ./partials/footer %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<% include ./partials/header %> | ||
|
||
<div class="ui main text container segment"> | ||
<div class="ui huge header">New Blog</div> | ||
<form class="ui form" action="/blogs" method="POST"> | ||
<div class="field"> | ||
<label>Title</label> | ||
<input type="text" name="blog[title]" placeholder="title"> | ||
</div> | ||
<div class="field"> | ||
<label>Image</label> | ||
<input type="text" name="blog[image]" placeholder="image"> | ||
</div> | ||
<div class="field"> | ||
<label>Blog Content</label> | ||
<textarea name="blog[body]"></textarea> | ||
</div> | ||
<input class="ui violet big basic button" type="submit"> | ||
</form> | ||
</div> | ||
|
||
<% include ./partials/footer %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<html> | ||
<head> | ||
<title>Blog App</title> | ||
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.4/semantic.min.css"> | ||
<link rel="stylesheet" type="text/css" href="/stylesheets/app.css"> | ||
</head> | ||
<body> | ||
<div class="ui fixed inverted menu"> | ||
<div class="ui container"> | ||
<div class="header item"><i class="code icon"></i>Blog Site</div> | ||
<a href="/" class="item">Home</a> | ||
<a href="/blogs/new" class="item">New Post</a> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<% include ./partials/header %> | ||
|
||
<div class="ui main text container segment"> | ||
<div class="ui huge header"><%= blog.title %></div> | ||
<div class="ui top attached"> | ||
<div class="item"> | ||
<img class="ui centered rounded image" src="<%= blog.image %>"> | ||
<div class="content"> | ||
<span><%= blog.created.toDateString() %></span> | ||
</div> | ||
<div class="description"> | ||
<p><%= blog.body %></p> | ||
</div> | ||
<a class="ui orange basic button" href="/blogs/ <%= blog._id %>/edit">Edit</a> | ||
<form id="delete" action="/blogs/<%= blog._id %>?_method=DELETE" method="POST"> | ||
<button class="ui red basic button">Delete</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<% include ./partials/footer %> |