Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,46 @@ app.get("/index", async (req, res) => {
});

app.get("/news", async (req, res) => {
var category = "general";
var API_KEY = process.env.API_KEY;
// categories = [business, entertainment, general, health, science, sports, technology]
var API_KEY = process.env.API_KEY;
var email = req.params.email;
var details = await User.findOne({ email });
var data_json = [];

if (details !== null) {
for (var x of Object.keys(details["userPreference"])) {
var category = details["userPreference"][x];
console.log(category);
if (category === true) {
var uri = `https://newsapi.org/v2/top-headlines?country=us&category=${category}&apiKey=${API_KEY}`;
res.header("Access-Control-Allow-Origin", "*");

axios.get(uri).then(function (response) {
var data = response.data;
data_json.push(data);
});
}
}
res.status(200).json(data_json);
} else res.status(401).json({ message: "User not found" });
});
// var category = "general";
// var API_KEY = process.env.API_KEY;
// categories = [business, entertainment, general, health, science, sports, technology]
var uri = `https://newsapi.org/v2/top-headlines?country=us&category=${category}&apiKey=${API_KEY}`;
res.header("Access-Control-Allow-Origin", "*");

axios
.get(uri)
.then(function (response) {
var data = response.data;
res.status(200).json({ data });
})
.catch(function (error) {
console.log(error);
res.status(400).json({ error });
});
});
// var uri = `https://newsapi.org/v2/top-headlines?country=us&category=${category}&apiKey=${API_KEY}`;
// res.header("Access-Control-Allow-Origin", "*");

// axios
// .get(uri)
// .then(function (response) {
// var data = response.data;
// res.status(200).json({ data });
// })
// .catch(function (error) {
// console.log(error);
// res.status(400).json({ error });
// });
//});

app.get("/news/:email", async (req, res) => {
// categories = [business, entertainment, general, health, science, sports, technology]
Expand Down