-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (34 loc) · 904 Bytes
/
index.js
File metadata and controls
41 lines (34 loc) · 904 Bytes
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
import express from "express";
import axios from "axios";
const app = express();
const port = 3000;
const apiKey = ""; // ENTER YOUR API-KEY HERE
// getting response from the api
let response = null;
async function getResponse() {
try {
response = await axios.get(`https://newsapi.org/v2/top-headlines?country=in&apiKey=${apiKey}`);
} catch (error) {
console.error(error)
}
}
getResponse();
app.use(express.static("public"));
app.get("/", (req, res) => {
try {
const titles = [];
for (const o of response.data.articles) {
titles.push(o.title);
}
res.render("index.ejs", { data: titles });
} catch (error) {
res.send("error");
console.error(error);
}
})
app.get("/response", (req, res) => {
res.send(response.data);
})
app.listen(port, () => {
console.log(`listening on port ${port}`);
});