-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
52 lines (51 loc) · 1.93 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
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const Blog = require("./models/blog");
mongoose.connect("mongodb://riteshponty:[email protected]:27017,ac-aztryrz-shard-00-01.kieyyok.mongodb.net:27017,ac-aztryrz-shard-00-02.kieyyok.mongodb.net:27017/?ssl=true&replicaSet=atlas-woodvy-shard-0&authSource=admin&retryWrites=true&w=majority").then(() => {
console.log('database connected');
}).catch((err) => {
console.log(err);
})
app.get("/", (req, res) => {
res.redirect("/add-blog")
})
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'Public')));
app.use(express.urlencoded({ extended: true }));
const port = 3000;
app.get("/show-blogs",async (req, res) => {
const allBlogs = await Blog.find({});
console.log(allBlogs)
res.render('Show_blogs',{allBlogs})
})
app.get('/add-blog', (req, res) => {
res.render("add_item");
})
app.post('/add-blog', async(req, res) => {
console.log(req.body);
const { fname, lname, email, title, image1,image2,image3,image4, content } = req.body;
const isdatastored=await Blog.create({ name: fname + " " + lname, email: email, photo: [image1,image2,image3,image4], text: content, title: title });
if (!isdatastored) {
console.log('Err');
}
console.log('Data stored Success');
res.redirect("/show-blogs")
})
app.get("/read-blog/:id",async(req, res) => {
const { id } = req.params;
const item = await Blog.findById(id);
console.log(item);
res.render('read_blogs',{item})
})
app.post("/delete-blog/:id",async (req, res) => {
const { id } = req.params;
const deleteitem=await Blog.findByIdAndDelete(id);
if (!deleteitem) {
console.log('Item not deleted');
}
console.log('Item deleted');
res.redirect("/show-blogs")
})
app.listen(port, () => { console.log('Server is running at port 3000') });