-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 1.69 KB
/
app.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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const authRoutes = require("./routes/authRoutes");
const axios = require("axios");
const cheerio = require("cheerio");
const addShareRoutes = require("./routes/addShareRoutes");
const port = process.env.PORT || 8000;
// database connection
const dbURI =
"mongodb+srv://aditi:lucknow@cluster0.ozq8fw3.mongodb.net/userInfo";
mongoose
.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then((result) =>
app.listen(port, console.log(`Listening on port ${port}...`))
)
.catch((err) => console.log(err));
// middlewares
app.use(cors());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
app.use(express.json());
// routes
app.use("/api", authRoutes);
app.use("/api/addshare", addShareRoutes);
//web-scraping
axios("https://www.moneycontrol.com/news/business/stocks/")
.then((res) => {
const htmlData = res.data;
const articles = []
const $ = cheerio.load(htmlData);
$(".clearfix", htmlData).each((index, element) => {
const title = $(element).children("h2").text();
const titleUrl = $(element).children("a").attr("href");
const imageUrl = $(element).children("a").children("img").attr("data");
if (
title !== undefined &&
titleUrl !== undefined &&
imageUrl !== undefined
) {
articles.push({
title,
titleUrl,
imageUrl,
});
}
});
app.get("/news", (req,res)=>{
res.send(articles)
})
})
.catch((err) => {
console.error(err);
});