-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (123 loc) · 3.34 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require('isomorphic-fetch');
const express = require('express');
const next = require('next');
const LRUCache = require('lru-cache');
const { HOST, PORT, NODE_ENV } = require('./constants');
const dev = NODE_ENV !== 'production';
const app = next({ dir: '.', dev });
const handle = app.getRequestHandler();
/**
* Initialize a LRU in-memory cache
*/
const cache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60 * 24,
});
/**
* Check if we already render the page and render it if not
* @method renderAndCache
* @param {Object} req The HTTP request
* @param {Object} res The HTTP response
* @param {string} pagePath The Next.js page path to render
* @param {Object} queryParams The query params to use in the render
*/
function renderAndCache(req, res, pagePath, queryParams) {
if (cache.has(req.url)) {
console.log(`CACHE HIT: ${req.url}`);
res.send(cache.get(req.url));
return;
}
app.renderToHTML(req, res, pagePath, queryParams)
.then((html) => {
console.log(`CACHE MISS: ${req.url}`);
cache.set(req.url, html);
res.send(html);
})
.catch((err) => {
app.renderError(err, req, res, pagePath, queryParams);
});
}
/**
* Check if we already fetched the data from the Github API and fetch it if not.
* @method fetchAndCache
* @param {Object} req The HTTP request
* @param {Object} res The HTTP response
* @param {string} apiURL The Github API to fetch
*/
function fetchAndCache(req, res, apiURL) {
if (cache.has(req.url)) {
console.log(`CACHE HIT: ${req.url}`);
res.send(cache.get(req.url));
return;
}
console.log(`CACHE MISS: ${req.url}`);
fetch(apiURL)
.then(response => response.json())
.then(data => {
cache.set(req.url, JSON.stringify(data));
res.json(data);
});
}
/**
* Start server
*/
app.prepare()
.then(() => {
const server = express();
/**
* Fetch data for the list of repos
*/
server.get('/api/repos', (req, res) => {
fetchAndCache(
req,
res,
'https://api.github.com/orgs/PlatziDev/repos?type=public'
);
});
/**
* Fetch the README of a single repo
*/
server.get('/api/repos/:name', (req, res) => {
const {
params: { name },
} = req;
fetchAndCache(
req,
res,
`https://api.github.com/repos/PlatziDev/${name}/readme`
);
});
/**
* Render the readme of a single repo (if is not dev use cache)
*/
server.get('/repo/:name', (req, res) => {
const params = Object.assign(req.query, req.params);
if (dev) return app.render(req, res, '/repo', params);
return renderAndCache(req, res, '/repo', params);
});
/**
* Render the list of repos (if is not dev use cache)
*/
server.get('/', (req, res) => {
const params = Object.assign(req.query, req.params);
if (dev) return app.render(req, res, '/', params);
return renderAndCache(req, res, '/', params);
});
/**
* Let next.js handle every other possible URL
*/
server.get('*', (req, res) => {
return handle(req, res);
});
/**
* Run server in the setted host and port (localhost and 3000 by default)
*/
server.listen(PORT, HOST, (err) => {
if (err) throw err;
console.log(`> Ready on http://${HOST}:${PORT}/`);
});
})
.catch(error => {
console.error(error);
process.exit(0);
});