-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (44 loc) · 1.17 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
const express = require('express');
const bodyParser = require('body-parser');
const Blog = require('./models/blog');
const mongoose = require('mongoose');
const app = express();
mongoose.connect("mongodb://localhost/angapp"
).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(documents =>{
res.status(200).json({
mess:"getting from db is done -success",
blogs:documents
})
});
});
module.exports = app;