-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (62 loc) · 1.46 KB
/
app.js
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
const express= require("express");
const bodyParser= require("body-parser");
const ejs= require("ejs");
var _ = require('lodash');
const mongoose= require("mongoose");
const { Schema } = mongoose;
const app= express();
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine","ejs");
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/blogDB");
const blogSchema= new Schema({
title: String,
ParaA: String,
ParaB: String,
ParaC: String
});
const Blog = mongoose.model('Blog', blogSchema);
app.get("/",function(req,res){
Blog.find({},function(err,docs){
if(err){
console.log(err);
} else{
res.render('body',{list: docs});
}
});
});
app.get("/home",function(req,res){
res.render('body');
});
app.get("/contact",function(req,res){
res.render('contact');
});
app.get("/projects",function(req,res){
res.render('projects');
});
app.get("/compose",function(req,res){
res.render('compose');
});
app.get("/:id",function(req,res){
Blog.findOne({_id: req.params.id},function(err,docs){
if(!err){
res.render('post',{blogPost: docs});
}
});
});
app.post("/compose",function(req,res){
const blogPage= new Blog({
title: req.body.title,
ParaA: req.body.A,
ParaB: req.body.B,
ParaC: req.body.C
});
blogPage.save(function(err){
if(!err){
res.redirect("/");
}
});
});
app.listen(3000,function(){
console.log("Server started at port 3000");
});