Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Full Stack Blogging Website #320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions projects/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');

const homeStartingContent = "Hey, this website is created by Aman Yadav, if you want to write somthing, click on the upper right corner i.e write button and start writing";

const aboutContent = "This web app is created by the use of HTML, CSS, JS, ExpressJS, EJS, Loodash";

const contactContent = "Mail to: [email protected]";

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/blogDB", {useNewUrlParser: true});

const postSchema = {
title: String,
content: String
};

const Post = mongoose.model("Post", postSchema);

app.get("/", function(req, res){

Post.find({}, function(err, posts){
res.render("home", {
startingContent: homeStartingContent,
posts: posts
});
});
});

app.get("/compose", function(req, res){
res.render("compose");
});

app.post("/compose", function(req, res){
const post = new Post({
title: req.body.postTitle,
content: req.body.postBody
});


post.save(function(err){
if (!err){
res.redirect("/");
}
});
});

app.get("/posts/:postId", function(req, res){

const requestedPostId = req.params.postId;

Post.findOne({_id: requestedPostId}, function(err, post){
res.render("post", {
title: post.title,
content: post.content
});
});

});

app.get("/about", function(req, res){
res.render("about", {aboutContent: aboutContent});
});

app.get("/contact", function(req, res){
res.render("contact", {contactContent: contactContent});
});


app.listen(3000, function() {
console.log("Server started on port 3000");
});
18 changes: 18 additions & 0 deletions projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ejs-challenge",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^3.1.9",
"express": "^4.16.3",
"lodash": "^4.17.11",
"mongoose": "^5.3.6"
}
}
44 changes: 44 additions & 0 deletions projects/public/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
html {
min-height: 100%;
position: relative;
}

.container-fluid {
padding-top: 70px;
padding-bottom: 70px;
}
.navbar {
padding-top: 15px;
padding-bottom: 15px;
border: 0;
border-radius: 0;
margin-bottom: 0;
font-size: 12px;
letter-spacing: 5px;
}
.navbar-nav li a:hover {
color: #1abc9c !important;
}

.footer-padding {
padding-bottom: 60px;
}

.footer {
position: absolute;
text-align: center;
bottom: 0;
width: 100%;
height: 60px;
background-color: #1abc9c;
}

.footer p {
margin-top: 25px;
font-size: 12px;
color: #fff;
}

.name{
color: white;
}
4 changes: 4 additions & 0 deletions projects/views/about.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%- include("partials/header"); -%>
<h1>About</h1>
<p> <%= aboutContent %> </p>
<%- include("partials/footer"); -%>
14 changes: 14 additions & 0 deletions projects/views/compose.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<%- include("partials/header"); -%>
<h1>Compose</h1>
<form class="" action="/compose" method="post">
<div class="form-group">
<label>Title</label>
<input class="form-control" type="text" name="postTitle">
<label>Post</label>
<textarea class="form-control" name="postBody" rows="5" cols="30"></textarea>
</div>
<button class="btn btn-primary" type="submit" name="button">Publish</button>
</form>

<%- include("partials/footer"); -%>
4 changes: 4 additions & 0 deletions projects/views/contact.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%- include("partials/header"); -%>
<h1>Contact</h1>
<p> <%= contactContent %> </p>
<%- include("partials/footer"); -%>
20 changes: 20 additions & 0 deletions projects/views/home.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<%- include("partials/header"); -%>

<h1>Home</h1>
<p> <%= startingContent %> </p>


<% posts.forEach(function(post){ %>

<h1><%=post.title%></h1>
<p>
<%=post.content.substring(0, 100) + " ..."%>
<a href="/posts/<%=post._id%>">Read More</a>
</p>


<% }) %>


<%- include("partials/footer"); -%>
9 changes: 9 additions & 0 deletions projects/views/partials/footer.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

</div>
<div class="footer-padding"></div>
<div class="footer">
<p>Made By <a class="name" href="https://linktr.ee/itsamanyadav">Aman Yadav</a></p>
</div>
</div>
</body>
</html>
26 changes: 26 additions & 0 deletions projects/views/partials/header.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title>Daily Journal</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/styles.css">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<p class="navbar-brand">Blogging Site</p>
</div>
<ul class="nav navbar-nav navbar-right">
<li id="home"><a href="/">HOME</a></li>
<li id="compose"> <a href="/compose">WRITE</a> </li>
<li id="about"><a href="/about">ABOUT US</a></li>
<li id="contact"><a href="/contact">CONTACT US</a></li>

</ul>
</div>
</nav>

<body>
<div class="container">
10 changes: 10 additions & 0 deletions projects/views/post.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<%- include("partials/header"); -%>


<h1><%=title%></h1>
<p><%=content%></p>



<%- include("partials/footer"); -%>
15 changes: 15 additions & 0 deletions projects/views/test.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%- include("partials/header"); -%>
<h1>Home</h1>
<p> <%= startingContent %> </p>


<% posts.forEach(function(post){ %>

<h1><%=post.title%></h1>
<p>
<%=post.content.substring(0, 100) + " ..."%>
<a href="/posts/<%=post.title%>">Read More</a>
</p>


<% }) %>