-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (80 loc) · 2.43 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
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
const express = require("express");
const app = express();
const path = require("path");
const departments = require('./json/departments.json');
const partners = require('./json/communitypartners.json');
const teams = require('./json/teams.json');
const axios = require('axios')
const { resolve } = require("path");
const { param } = require("express/lib/request");
const apiUrl = 'https://gdscuitrgpv-backend.herokuapp.com'
const router = express.Router();
app.set('views', path.join(__dirname, 'templates'));
app.set('view engine', 'ejs');
getData = async (path) => {
try {
const response = await axios.get(apiUrl +'/api/'+ path).then(res => res.data);
return response.data;
} catch (error) {
console.log(error.data);
}
}
// use public folder for static files
app.use(express.static(path.join(__dirname, 'public')));
router.get("/", async (req, res) => {
const teamsData = await getData(`teams?populate=*`)
const departmentsData = await getData(`departments?populate=*`)
const partnersData = await getData(`communitypartners?populate=*`)
const blogsData = await getData(`blogs?populate=*`)
const eventsData = await getData(`events?populate=*`)
const feedbackData = await getData(`feedbacks?populate=*`)
const params = {
departments: departmentsData,
partners: partnersData,
teams: teamsData,
blogs: blogsData,
events: eventsData,
feedbacks: feedbackData,
apiUrl: apiUrl
}
// res.send(params)
res.render("index", params);
});
router.get("/contact", (req, res) => {
res.render("contact",{
apiUrl: apiUrl
});
});
// Blogs routes
router.get("/blog/:slug", async (req, res) => {
const slug = req.params.slug;
const blogsData = await getData(`blogs?populate=*`)
const blog = blogsData.find(blog => blog.attributes.slug === slug);
if(blog){
const params = {
blog: blog,
apiUrl: apiUrl
}
// res.send(params);
res.render("blog", params);
}
else{
res.send('No blog found for slug: '+ slug)
}
});
// All Blogs Route
router.get("/blogs", async (req, res) => {
const blogsData = await getData(`blogs?populate=*`)
const params = {
blogs: blogsData,
apiUrl: apiUrl
}
// res.send(params);
res.render("blogs", params);
});
router.get("/about", (req, res) => {
res.render("about", { title: "Hey", message: "Hello there!" });
});
app.use("/", router);
app.listen(process.env.port || 8000);
console.log("Running at Port 8000");