-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
96 lines (79 loc) · 2.63 KB
/
server.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var express = require('express');
var path = require("path");
var bodyParser = require('body-parser');
var userSchema=require("./models/user");
var mongoose = require("mongoose");
var Blog;
var Category;
var db = mongoose.connect("mongodb://localhost:27017/angDB", function(err, response){
if(err){ console.log( err); }
else{ //console.log('Connected to ' + db, ' + ', response);
console.log("connection established successfully")
Blog = mongoose.model("Blogs",userSchema.Blog,"blogs");
Category=mongoose.model("Categories",userSchema.Category,"categories");
Blog.find({},(err,data)=>{
if(err)console.log(err);
else{
// console.log(data);
console.log("hi")
}
})
}
});
var app = express()
app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200'); //// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // methods to allowed
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.get("/api/blog",(req,res)=>{
Blog.find({},(err,data)=>{
if(err)console.log(err);
else{
res.setHeader("content-type","application/json")
// console.log(data);
res.send(JSON.stringify(data));
}
})
})
app.get("/api/featured_blogs",(req,res)=>{
Blog.find({},(err,data)=>{
if(err)console.log(err);
else{
console.log("featured-blog")
res.setHeader("content-type","application/json");
res.send(JSON.stringify(data));
}
})
})
app.get("/api/recent_blogs/",(req,res)=>{
Blog.find({},(err,data)=>{
if(err)console.log(err);
else{
console.log("recent-blog")
res.setHeader("content-type","application/json");
res.send(JSON.stringify(data));
}
})
})
app.get("/api/blog/:ids",(req,res)=>{
console.log("Hi");
var id=req.params.ids;// to access the parameters
console.log(id);
Blog.find({id:id},(err,data)=>{
if(err)console.log(err);
else{
console.log(data);
res.setHeader("content-type","applicaition/json");
res.send(JSON.stringify(data));
}
})
})
app.listen(3000,()=>{
console.log("server on");
})