-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (78 loc) · 3.01 KB
/
Copy pathapp.js
File metadata and controls
95 lines (78 loc) · 3.01 KB
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
95
// getting the dependencies------------------------------------------------------------------------------
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const request = require('request');
const links = require('./routes/links');
const config = require('./config/database');
const articleSchema = require('./models/articles');
var CronJob = require('cron').CronJob;
//connection to the database-----------------------------------------------------------------------------
mongoose.connect(config.link);
mongoose.connection.on('connected', function(){
console.log('connected to database');
});
//setting up the app-------------------------------------------------------------------------------------
var app = express();
app.use(cors());
//setup paths----------------------------------------------------------------------------------------
app.use('/', links);
//middleware integration----------------------------------------------------------------------------------
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname,'public'))); // middleware for serving the assets
//application server-------------------------------------------------------------------------------------
app.listen(3000, function(){
console.log('Server started on port 3000');
});
process.on('UnhandledPromiseRejectionWarning', function (err) {
console.log(err);
});
//cron fetching the data for the headlines--------------------------------------------------------------
new CronJob('00 00 */12 * * *', function() {
console.log('fetching news')
//API request---------------------------------------------------------------------------------------------
const options = {
url:'https://newsapi.org/v2/everything?language=en&sources=cnn,abc-news,bbc-news,bbc-sport,espn,business-insider,cnbc&apiKey=edb738ea05fa4b39b2f0958b54e4115c' ,
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
}
};
let articles;
request(options, function(err, res, body) {
if(err){
console.log('ERROR');
}else{
let json = JSON.parse(body);
articles=json.articles;
articles.forEach(element =>{
saveToDb(element);
console.log(element);
});
}
});
//saving articles to the database------------------------------------------------------------------------
let saveToDb= function(attr){
let tempSource = attr.source.name;
let tempTopic = attr.title;
let tempDesc = attr.description;
let tempImURL = attr.urlToImage;
let tempNewsURL = attr.url;
let newArticle = new articleSchema({
source: tempSource,
topic: tempTopic,
description: tempDesc,
imageURL : tempImURL,
newsURL : tempNewsURL
});
newArticle.save((err)=>{
if(err){
console.log('error occured');
}
});
}
}, null, true, 'Asia/Kolkata');