Skip to content

Commit 33b30da

Browse files
committed
Add Create route to submit a new blog in RESTfulBlogApp
1 parent fb6d997 commit 33b30da

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

RESTfulBlogApp/app.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ app.get("/", function(req, res) {
3030
res.redirect("/blogs");
3131
});
3232

33+
// INDEX ROUTE
3334
app.get("/blogs", function(req, res) {
3435
Blog.find({}, function(err, blogs) {
3536
if(err) {
@@ -40,7 +41,24 @@ app.get("/blogs", function(req, res) {
4041
});
4142
});
4243

44+
// NEW ROUTE
45+
app.get("/blogs/new", function(req, res) {
46+
res.render("new");
47+
});
4348

49+
// CREATE ROUTE
50+
app.post("/blogs", function(req, res) {
51+
// create blog
52+
Blog.create(req.body.blog , function(err, newBlog) {
53+
if(err) {
54+
res.render("new");
55+
} else {
56+
// redirect to the index
57+
res.redirect("/blogs");
58+
}
59+
});
60+
61+
});
4462

4563
app.listen(3000, function() {
4664
console.log("Server has started at PORT 3000");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
i.icon {
22
font-size: 2em;
3+
}
4+
5+
.container.main {
6+
margin-top: 7.0em;
37
}

RESTfulBlogApp/views/new.ejs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<% include partials/header %>
2+
3+
<div class="ui main text container segment">
4+
<div class="ui huge header">New Blog</div>
5+
<form class="ui form" action="/blogs" method="POST">
6+
<div class="field">
7+
<label>Title</label>
8+
<input type="text" name="blog[title]" placeholder="title">
9+
</div>
10+
<div class="field">
11+
<label>Image</label>
12+
<input type="text" name="blog[image]" placeholder="image">
13+
</div>
14+
<div class="field">
15+
<label>Title</label>
16+
<textarea name="blog[body]"></textarea>
17+
</div>
18+
<input class="ui big violet basic button" type="submit">
19+
</form>
20+
</div>
21+
<% include partials/footer %>

RESTfulBlogApp/views/partials/header.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Blog App</title>
55
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.css">
6-
<link rel="stylesheet" type="text/css" href="stylesheets/app.css">
6+
<link rel="stylesheet" type="text/css" href="/stylesheets/app.css">
77
</head>
88
<body>
99
<div class="ui fixed inverted menu">

0 commit comments

Comments
 (0)