-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (41 loc) · 1.34 KB
/
index.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
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const axios = require('axios');
const app = express();
app.use(express.json());
app.use(cors());
dotenv.config();
const port = process.env.PORT || 2000;
//Starting Server
app.listen(port, () => {
console.log("Server Listening on Port : ", port)
})
//Function to get Blog Categories
const getCategories = async () => {
var temp = await axios.get(process.env.BLOG_URL);
return temp.data.blogs;
}
const getArticlesForCategory = async (blog_id) => {
var temp = await axios.get(`${process.env.ARTICLE_URL}/${blog_id}/articles.json?fields=id,title,body_html,author,published_at,summary_html,image`);
return temp.data;
}
const groupArticles = async (blogCategories) => {
const promises = blogCategories.map(async (category) => {
const tempArticles = await getArticlesForCategory(category.id);
return {
blog_title: category.title,
blog_id: category.id,
articles: tempArticles.articles,
};
});
const articles = await Promise.all(promises);
return articles;
};
//GET Requests for fetching Data
app.get("/getBlogs", async (req, res) => {
const blogCategories = await getCategories();
const response = await groupArticles(blogCategories);
res.json(response);
})
app.use('/assets', express.static('assets'));