-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
40 lines (35 loc) · 1.15 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
const express = require('express');
const app = express();
const bodyParser= require('body-parser');
const morgan = require('morgan');
const request = require('request');
// var newsEver = require('./model/api_everything.js')
// var newsSrc = require('./model/api_sources.js')
// var newsHead = require('./model/api_headlines.js')
const PORT = process.env.PORT || 3000;
app.set('view engine','ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(morgan('tiny'));
app.get('/', (req,res) => {
res.render('index.ejs');
});
app.get('/req/:cat', (req,res) => {
var url = "https://newsapi.org/v2/sources?category="+req.params.cat+"&apiKey=f59f72ac08824e4e921cd9c69f519ccf";
request(url,function(err,response,body){
if(!err && response.statusCode==200){
console.log("Success");
var newsArr = JSON.parse(body).sources;
// console.log(news)
res.render("newscat",{newsArr:newsArr})
}
else{
console.log("Something is not right");
res.status(404).send(); //404 - Resource or page not found error
}
})
// res.render('index.ejs');
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});