-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (97 loc) · 3.12 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
const express = require('express');
const next = require('next');
const path = require('path');
const fs = require('fs');
const https = require('https');
const fetch = require('isomorphic-unfetch');
const config = require('./config/config.json');
const connectDb = require('./utilities/connectDb');
const router = express.Router();
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const CITY = 'New Orleans';
function getNewsDesk(newsDesk) {
if (newsDesk === 'sports') {
return { $or: [
{ news_desk: 'Sports' },
{ section_name: 'Sports' },
] };
}
return { $and: [
{ news_desk: { $ne: 'Sports' } },
{ section_name: { $ne: 'Sports' } },
] };
}
app.prepare()
.then(() => {
const server = express();
server.get(['/sitemap', '/sitemap.xml'], (req, res) => {
res.sendFile(path.join(__dirname, '/static', '/sitemap.xml'));
});
server.get(['/robots', '/robots.txt'], (req, res) => {
res.sendFile(path.join(__dirname, '/static', '/robots.txt'));
});
server.get(config.certbot.endpoint, (req, res) => {
res.send(config.certbot.key);
});
router.route('/articles').get((req, res) => {
if (!req.query.newsDesk) {
return response.send('Looks like you did not send a proper request');
}
const query = getNewsDesk(req.query.newsDesk);
const LIMIT = 10;
const skip = (req.query && req.query.page * LIMIT) || 0;
connectDb()
.then(db => {
db.collection('articles').find(query).count().then(total => {
if (total < (skip + LIMIT)) {
return res.json({ message: 'out' });
}
db.collection('articles').find(query).sort({ pub_date: -1 }).skip(skip).limit(LIMIT).toArray((err, docs) => {
res.json(docs);
});
});
})
.catch(error => console.log(error));
});
router.route('/weather').get((req, res) => {
const BASE_REQUEST = 'http://api.openweathermap.org/data/2.5/';
const WEATHER = 'weather?';
const FORECAST = 'forecast/daily?';
let query = req.query.zipCode ? 'zip=' + req.query.zipCode : 'q=' + CITY;
query += 'us&units=imperial&APPID=' + config.openWeather.key;
const promises = [
fetch(BASE_REQUEST + WEATHER + query),
fetch(BASE_REQUEST + FORECAST + query),
];
Promise.all(promises)
.then(responses => {
const jsonPromises = responses.map(r => r.json())
return Promise.all(jsonPromises);
})
.then(weather => res.json(Object.assign({}, weather[0], { forecast: weather[1] })))
.catch(error => console.log(error));
});
server.use('/api', router);
server.get('*', (req, res) => {
return handle(req, res);
});
if (dev) {
server.listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
} else {
console.log('using production configs');
const key = fs.readFileSync('./privkey.pem');
const cert = fs.readFileSync('./cert.pem');
https.createServer({
key,
cert,
}, server).listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on https://localhost:3000');
});
}
});