Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (49 loc) · 1.48 KB
/
Copy pathapp.js
File metadata and controls
79 lines (49 loc) · 1.48 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
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require("lodash");
const mongoose = require("mongoose");
require('dotenv').config();
const nodemailer = require("nodemailer");
// http://localhost:3000/blog
//--------db connection ------------
const password = process.env.mongopassword;
mongoose.connect("mongodb+srv://amit-samui:" + password + "@cluster0.nuddc.mongodb.net/blogdb" , {useNewUrlParser: true});
const postSchema = {
blogTitle: String,
blogContent: String,
blogName: String,
blogEmail: String
}
const Post = mongoose.model("Post" , postSchema);
//--------- db connection end----------
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("main");
});
// ---------------------------------blog section ---------------------
app.get("/blog" , (req , res) => {
Post.find({} , (err , posts) => {
if(err){console.log(err)}
else{
res.render("blog" , {blogs : posts} );
}
})
})
app.post("/blog" , (req , res) => {
const body = req.body;
const post = new Post({
blogTitle: body.blogTitle,
blogContent: body.blogContent,
blogName: body.blogName,
blogEmail: body.blogEmail
})
post.save();
res.redirect("/blog")
})
app.listen(process.env.PORT || 3000 , () => {
console.log("the server is running at port 3000");
})