-
Notifications
You must be signed in to change notification settings - Fork 0
/
app1.js
61 lines (51 loc) · 1.44 KB
/
app1.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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const Blog = require('./models/blog');
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/angapp", { useNewUrlParser: true }
)
.then(() => {
console.log("Connected to DB Successfully!!");
})
.catch(() => {
console.log("Error in connecting to DB!");
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', "*");
res.setHeader('Access-Control-Allow-Headers',
"Origin, X-Requested-With, Content-Type, Accept");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE, OPTIONS"
);
next();
})
app.post("/api/blogs", (req, res, next) => {
const blog = new Blog({
title: req.body.title,
content: req.body.content
});
blog.save();
//console.log(blog);
res.status(201).json({
message: 'blog added successfully'
});
});
app.get('/api/blogs', (req, res) => {
Blog.find().then(documents => {
res.status(200).json({
mess: "Post is done -success",
blogs: documents
})
});
});
app.delete('/api/blogs/:id',(req,res,next)=>{
Blog.deleteOne({_id:req.params.id}).then(result =>{
console.log(result);
res.status(200).json({message:"Blog Review deleted!"})
});
});
module.exports = app;