-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (83 loc) · 2.92 KB
/
server.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Using template for Node app on Heroku:
// https://github.com/heroku/node-js-getting-started/blob/main/index.js
const express = require('express');
const cors = require('cors');
const request = require('request');
// Helper functions for generating API query strings
const newsQuery = require('./lib/newsQuery.js');
const app = express();
// Check for development environment
if (process.env.NODE_ENV !== 'production') {
// Use dotenv file for local API key storage (.env file)
require('dotenv').config();
// Set up Webpack hot reloading for development environment
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.dev.js');
const compiler = webpack(config);
app.use(webpackHotMiddleware(compiler));
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
}
// Get port from environment or else default to 5000
app.set('port', (process.env.PORT || 5001));
// Enable all CORS requests
// app.use(cors());
// Read files from this folder
app.use(express.static(__dirname + '/dist'));
app.get('/', function(req, res) { // request, response
res.render('index.html');
});
// API ---
function pipeRequest(req, res, url) {
req.pipe(request(url)).pipe(res);
}
function pipeSourceByTop(req, res, source) {
pipeRequest(req, res, newsQuery.sourceByTop(source));
}
function pipeCountryByTop(req, res, country) {
pipeRequest(req, res, newsQuery.countryByTop(country));
}
app.get('/api/top', cors(), function(req, res) {
// pipeSourceByTop(req, res, 'google-news');
pipeCountryByTop(req, res, 'us');
});
app.get('/api/arstechnica', cors(), function(req, res) {
pipeSourceByTop(req, res, 'ars-technica');
});
app.get('/api/associatedpress', cors(), function(req, res) {
pipeSourceByTop(req, res, 'associated-press');
});
app.get('/api/cnn', cors(), function(req, res) {
pipeSourceByTop(req, res, 'cnn');
});
app.get('/api/espn', cors(), function(req, res) {
pipeSourceByTop(req, res, 'espn');
});
app.get('/api/fortune', cors(), function(req, res) {
pipeSourceByTop(req, res, 'fortune');
});
app.get('/api/reuters', cors(), function(req, res) {
pipeSourceByTop(req, res, 'reuters');
});
app.get('/api/techcrunch', cors(), function(req, res) {
pipeSourceByTop(req, res, 'techcrunch');
});
app.get('/api/wallstreetjournal', cors(), function(req, res) {
pipeSourceByTop(req, res, 'the-wall-street-journal');
});
app.get('/api/time', cors(), function(req, res) {
pipeSourceByTop(req, res, 'time');
});
app.get('/api/usatoday', cors(), function(req, res) {
pipeSourceByTop(req, res, 'usa-today');
});
// This wildcard method (catch-all) handles all other requests - keep on bottom
app.get('*', function (req, res) {
res.redirect('/');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});