-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (68 loc) · 2.01 KB
/
app.js
File metadata and controls
95 lines (68 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
var _ = require('lodash');
const { lowerCase } = require("lodash");
const mongoose = require("mongoose");
const homeStartingContent = "Welcome to my Blog Page. I write about my daily life here. Start knowing me by wandering in this blog....";
const aboutContent = "Hello !\n I am Kedar Deshpande from India. I am pursuing iMTech in ECE from IIIT Bangalore.";
const contactContent = "Contact me on my email: kdeshpande2001@gmail.com";
let posts=[];
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');
const postSchema={
title: String,
content: String
};
const Post = mongoose.model("Post",postSchema);
app.get("/",function(req,res){
Post.find({},function(err,postArray){
if(!err && postArray)
posts=postArray;
const jsobjHome = {
homeStartingContent: homeStartingContent,
posts: posts
};
res.render("home",jsobjHome);
});
});
app.get("/about",function(req,res){
const jsobjAbout = {
aboutContent: aboutContent
};
res.render("about",jsobjAbout);
});
app.get("/contact",function(req,res){
const jsobjContact = {
contactContent: contactContent
};
res.render("contact",jsobjContact);
});
app.get("/compose",function(req,res){
res.render("compose");
});
app.get("/posts/:postId",function(req,res){
Post.findOne({_id: req.params.postId},function(err,post){
if(!err && post)
res.render("post",post);
});
});
app.post("/compose",function(req,res){
const newPost = {
title: req.body.postTitle,
content: req.body.postContent
};
const post=new Post(newPost);
post.save(function(err){
if(!err)
res.redirect("/");
});
});
//sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
app.listen(3000, function() {
console.log("Server started on port 3000");
});